我正在使用arrayadapter在listview上顯示數據。我已編碼的內容如下所示bt我得到空指針異常。請告訴我我錯過了什麼?列表視圖適配器中的Nullpointer異常
ArrayAdapter<String> adapter;
setContentView(R.layout.menu_home_activity);
OptionsList = (ListView) findViewById(R.id.OptionsLists);
String[] array = getResources().getStringArray(R.array.personal_settings);
adapter = new ArrayAdapter<String>(MenuHomeActivity.this,
R.layout.menu_home_activity, R.id.text, array);
OptionsList.setAdapter(adapter);
menu_home_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:name="@+id/OptionsLists"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
logcat的說:
03-14 10:54:59.242: E/AndroidRuntime(267): Uncaught handler: thread main exiting due to uncaught exception
03-14 10:54:59.252: E/AndroidRuntime(267): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.biz.elife/com.biz.elife.MenuHomeActivity}: java.lang.NullPointerException
03-14 10:54:59.252: E/AndroidRuntime(267): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
03-14 10:54:59.252: E/AndroidRuntime(267): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
03-14 10:54:59.252: E/AndroidRuntime(267): at android.app.ActivityThread.access$2200(ActivityThread.java:119)
03-14 10:54:59.252: E/AndroidRuntime(267): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
03-14 10:54:59.252: E/AndroidRuntime(267): at android.os.Handler.dispatchMessage(Handler.java:99)
03-14 10:54:59.252: E/AndroidRuntime(267): at android.os.Looper.loop(Looper.java:123)
03-14 10:54:59.252: E/AndroidRuntime(267): at android.app.ActivityThread.main(ActivityThread.java:4363)
03-14 10:54:59.252: E/AndroidRuntime(267): at java.lang.reflect.Method.invokeNative(Native Method)
03-14 10:54:59.252: E/AndroidRuntime(267): at java.lang.reflect.Method.invoke(Method.java:521)
03-14 10:54:59.252: E/AndroidRuntime(267): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
03-14 10:54:59.252: E/AndroidRuntime(267): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
03-14 10:54:59.252: E/AndroidRuntime(267): at dalvik.system.NativeStart.main(Native Method)
03-14 10:54:59.252: E/AndroidRuntime(267): Caused by: java.lang.NullPointerException
03-14 10:54:59.252: E/AndroidRuntime(267): at com.biz.elife.MenuHomeActivity.onCreate(MenuHomeActivity.java:33)
03-14 10:54:59.252: E/AndroidRuntime(267): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-14 10:54:59.252: E/AndroidRuntime(267): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
03-14 10:54:59.252: E/AndroidRuntime(267): ... 11 more
編輯:
33行是
OptionsList.setAdapter(adapter);
當我嘗試設置適配器列出提示錯誤,如果我刪除此行不顯示任何錯誤,也都顯示在屏幕上沒有任何部件只出現黑屏
謝謝大家的寶貴時間和回信我的錯誤是在menu_home_activity我加入android:name="@+id/OptionsList"
我改爲
android:id="@+id/OptionsList"
什麼是第33行的代碼 – 2012-03-14 05:27:01
你確定你有menu_home_activity的listview嗎?並在menuhomeactivity>中看到第33行。 – 2012-03-14 05:28:35
請參閱編輯 – Shruti 2012-03-14 05:31:02