Question

I was trying to clean up my listener(s) and got something wrong: I get a typical 'NullPoinerException' in LogCat w/the bellow methods (first time trying to do it this way).

Any help? Thnx!

The Activity:

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
Was it helpful?

Solution

Declare the acItem,adItem,cfrItem (as member vars if you like) then assign them in 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);
}

OTHER TIPS

In onCreate, you seem to be accessing acItem, adItem, and cfrItem without initializing them.

In onCreate, you seem to be accessing acItem, adItem, and cfrItem without initializing them.

well done ,it works for me

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top