OnClickListener NullPoinerException错误?

时间:2011-05-05 01:20:14

标签: android clicklistener

我试图清理我的监听器并出错了:我在LogCat中得到了一个典型的'NullPoinerException',并且有下面的方法(第一次尝试这样做)。

有任何帮助吗?日Thnx!

活动:

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

3 个答案:

答案 0 :(得分:3)

声明acItem,adItem,cfrItem(如果你愿意,可以作为成员变量) 然后在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);
}

答案 1 :(得分:2)

onCreate中,您似乎正在访问acItemadItemcfrItem而未初始化它们。

答案 2 :(得分:0)

在onCreate中,您似乎在不初始化acItem,adItem和cfrItem的情况下访问它们。

做得好,对我有用