2015-03-31 124 views
0

我想創建一個ListView,但適配器似乎崩潰導致零點異常的Android setAdapter空對象引用

代碼:

public void navMenuSetup() 
{ 
    mDrawerLayout = (DrawerLayout)findViewById(R.id.nav_layout); 
    mDrawerList = (ListView)findViewById(R.id.nav_listview); 
    navDrawerItems = new ArrayList<String>(); 
    String[] navMenuItem = new String[]{"about","Stories","Donate","Gallery"}; 

    navDrawerItems.add(navMenuItem[0]); 
    navDrawerItems.add(navMenuItem[1]); 
    navDrawerItems.add(navMenuItem[2]); 
    navDrawerItems.add(navMenuItem[3]); 

    ArrayAdapter<String> itemsAdapter = new ArrayAdapter<String>(this,android.R.layout.activity_list_item,navMenuItem); 
    mDrawerList.setAdapter(itemsAdapter); 

    getActionBar().setDisplayHomeAsUpEnabled(true); 
    getActionBar().setHomeButtonEnabled(true); 




} 

的logcat:

03-31 00:39:42.548 15014-15014/actionandroid.aa.com.actionandroid E/AndroidRuntime﹕ FATAL EXCEPTION: main 
    Process: actionandroid.aa.com.actionandroid, PID: 15014 
    java.lang.RuntimeException: Unable to start activity ComponentInfo{actionandroid.aa.com.actionandroid/actionandroid.aa.com.actionandroid.AboutActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference 
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298) 
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360) 
      at android.app.ActivityThread.access$800(ActivityThread.java:144) 
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278) 
      at android.os.Handler.dispatchMessage(Handler.java:102) 
      at android.os.Looper.loop(Looper.java:135) 
      at android.app.ActivityThread.main(ActivityThread.java:5221) 
      at java.lang.reflect.Method.invoke(Native Method) 
      at java.lang.reflect.Method.invoke(Method.java:372) 
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 
    Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference 
      at actionandroid.aa.com.actionandroid.AboutActivity.navMenuSetup(AboutActivity.java:44) 
      at actionandroid.aa.com.actionandroid.AboutActivity.onCreate(AboutActivity.java:59) 
      at android.app.Activity.performCreate(Activity.java:5933) 
      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105) 
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251) 
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360) 
            at android.app.ActivityThread.access$800(ActivityThread.java:144) 
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278) 
            at android.os.Handler.dispatchMessage(Handler.java:102) 
            at android.os.Looper.loop(Looper.java:135) 
            at android.app.ActivityThread.main(ActivityThread.java:5221) 
            at java.lang.reflect.Method.invoke(Native Method) 
            at java.lang.reflect.Method.invoke(Method.java:372) 
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) 
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 

它似乎是由使用空對象的arrayAdapter引起的,但在我的生活中找出了什麼是空的。

+0

請發佈您的日誌。 – 2015-03-31 00:20:18

+1

mDrawerList爲空。你發佈的代碼很難說爲什麼。請發佈您的佈局和您的活動的onCreate方法。 – androidevil 2015-03-31 01:55:01

回答

0

你忘記調用的setContentView您的活動?如果確實如此,請檢查您參考的佈局是否包含帶有編號的列表nav_listview