Question

J'essayais de nettoyer mes auditeurs et j'ai mal mal: je reçois une «nullpoinerexception» typique dans logcat avec les méthodes ci-dessous (la première fois en essayant de le faire de cette façon).

De l'aide? Thnx!

L'activité:

public class List_RegulatoryDocs extends Activity {

OnClickListener myClickListener = new OnClickListener() {
    @Override
    public void onClick(View v) {
        if (v == acItem) {
            Intent ACList = new Intent(List_RegulatoryDocs.this,
                    List_AC.class);
            startActivity(ACList);
        } else if (v == adItem) {
            // TODO
        }else if (v == cfrItem) {
            // TODO
        }//TODO + items
    }
};

/**
 * -- Called when the activity is first created
 * ===================================================================
 */
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.regulatory_docs_list);

    // Set the Click Listeners
    acItem.setOnClickListener(myClickListener);
    adItem.setOnClickListener(myClickListener);
    cfrItem.setOnClickListener(myClickListener);

}

/**
 * -- Local Variables
 * =====================================================================
 **/
RelativeLayout acItem = (RelativeLayout) findViewById(R.id.acItem);
RelativeLayout adItem = (RelativeLayout) findViewById(R.id.adItem);
 RelativeLayout cfrItem = (RelativeLayout) findViewById(R.id.cfrItem);
// RelativeLayout pmaItem = (RelativeLayout) findViewById(R.id.pmaItem);
// RelativeLayout saibItem = (RelativeLayout) findViewById(R.id.saibItem);
// RelativeLayout sfarItem = (RelativeLayout) findViewById(R.id.sfarItem);
// RelativeLayout stcItem = (RelativeLayout) findViewById(R.id.stcItem);
// RelativeLayout tsoItem = (RelativeLayout) findViewById(R.id.tsoItem);
// RelativeLayout tcdsItem = (RelativeLayout) findViewById(R.id.tcdsItem);
}

Logcat:

05-05 01:27:29.931: WARN/dalvikvm(13268): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
05-05 01:27:29.971: ERROR/AndroidRuntime(13268): FATAL EXCEPTION: main
05-05 01:27:29.971: ERROR/AndroidRuntime(13268): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.aeroTechnologies.flyDroid/com.aeroTechnologies.flyDroid.regList.List_RegulatoryDocs}: java.lang.NullPointerException
05-05 01:27:29.971: ERROR/AndroidRuntime(13268):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
05-05 01:27:29.971: ERROR/AndroidRuntime(13268):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
05-05 01:27:29.971: ERROR/AndroidRuntime(13268):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
05-05 01:27:29.971: ERROR/AndroidRuntime(13268):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
05-05 01:27:29.971: ERROR/AndroidRuntime(13268):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-05 01:27:29.971: ERROR/AndroidRuntime(13268):     at android.os.Looper.loop(Looper.java:123)
05-05 01:27:29.971: ERROR/AndroidRuntime(13268):     at android.app.ActivityThread.main(ActivityThread.java:4627)
05-05 01:27:29.971: ERROR/AndroidRuntime(13268):     at java.lang.reflect.Method.invokeNative(Native Method)
05-05 01:27:29.971: ERROR/AndroidRuntime(13268):     at java.lang.reflect.Method.invoke(Method.java:521)
05-05 01:27:29.971: ERROR/AndroidRuntime(13268):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-05 01:27:29.971: ERROR/AndroidRuntime(13268):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-05 01:27:29.971: ERROR/AndroidRuntime(13268):     at dalvik.system.NativeStart.main(Native Method)
05-05 01:27:29.971: ERROR/AndroidRuntime(13268): Caused by: java.lang.NullPointerException
05-05 01:27:29.971: ERROR/AndroidRuntime(13268):     at android.app.Activity.findViewById(Activity.java:1637)
05-05 01:27:29.971: ERROR/AndroidRuntime(13268):     at com.aeroTechnologies.flyDroid.regList.List_RegulatoryDocs.<init>(List_RegulatoryDocs.java:51)
05-05 01:27:29.971: ERROR/AndroidRuntime(13268):     at java.lang.Class.newInstanceImpl(Native Method)
05-05 01:27:29.971: ERROR/AndroidRuntime(13268):     at java.lang.Class.newInstance(Class.java:1429)
05-05 01:27:29.971: ERROR/AndroidRuntime(13268):     at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
05-05 01:27:29.971: ERROR/AndroidRuntime(13268):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)
05-05 01:27:29.971: ERROR/AndroidRuntime(13268):     ... 11 more
Était-ce utile?

La solution

Déclarez l'ACITEM, ADITEM, CFRITEM (en tant que membre VARS si vous le souhaitez) puis attribuez-les dans OnCreate ();

  public class List_RegulatoryDocs extends Activity {
RelativeLayout acItem ;
RelativeLayout adItem;
 RelativeLayout cfrItem ;
OnClickListener myClickListener = new OnClickListener() {
    @Override
    public void onClick(View v) {
        if (v == acItem) {
            Intent ACList = new Intent(List_RegulatoryDocs.this,
                    List_AC.class);
            startActivity(ACList);
        } else if (v == adItem) {
            // TODO
        }else if (v == cfrItem) {
            // TODO
        }//TODO + items
    }
};

/**
 * -- Called when the activity is first created
 * ===================================================================
 */
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.regulatory_docs_list);
acItem = (RelativeLayout) findViewById(R.id.acItem);
adItem = (RelativeLayout) findViewById(R.id.adItem);
 cfrItem = (RelativeLayout) findViewById(R.id.cfrItem);
    // Set the Click Listeners
    acItem.setOnClickListener(myClickListener);
    adItem.setOnClickListener(myClickListener);
    cfrItem.setOnClickListener(myClickListener);

}

/**
 * -- Local Variables
 * =====================================================================
 **/

// RelativeLayout pmaItem = (RelativeLayout) findViewById(R.id.pmaItem);
// RelativeLayout saibItem = (RelativeLayout) findViewById(R.id.saibItem);
// RelativeLayout sfarItem = (RelativeLayout) findViewById(R.id.sfarItem);
// RelativeLayout stcItem = (RelativeLayout) findViewById(R.id.stcItem);
// RelativeLayout tsoItem = (RelativeLayout) findViewById(R.id.tsoItem);
// RelativeLayout tcdsItem = (RelativeLayout) findViewById(R.id.tcdsItem);
}

Autres conseils

Dans onCreate, vous semblez accéder acItem, adItem, et cfrItem sans les initialiser.

Dans OnCreate, vous semblez accéder à ACITEM, ADITEM et CFRITEM sans les initialiser.

Bravo, ça marche pour moi

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top