我正在尝试创建一个ListView,但是适配器似乎崩溃导致空点异常
代码:
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使用null对象引起的,但是在我的生活中找出什么是null。
答案 0 :(得分:0)
问题似乎与您创建ListView的方式有关。我不能完全从你发布的代码中说出来但是Udacity对android dev有很好的介绍,他们会通过ArrayAdapter创建一个ListView。
答案 1 :(得分:0)
您是否忘记在活动中调用setContentView?如果您这样做,请检查您引用的布局是否包含具有id nav_listview
的列表