我正在使用android listview和它的工作完美。我的實施如下Android:listview崩潰
ListView listview = (ListView)findViewById(R.id.list);
setListAdapter(new ArrayAdapter<String>(MyIncidentActivity.this,
R.layout.row_incident, R.id.label_incident, db_results));
現在我介紹onItemClickListener如下,應用程序崩潰無故。
listview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
}
});
logcat的
08-12 14:44:40.105: ERROR/AndroidRuntime(14525): FATAL EXCEPTION: main
08-12 14:44:40.105: ERROR/AndroidRuntime(14525): java.lang.RuntimeException: Unable to start activity ComponentInfo{a.b/a.b.MyIncidentActivity}: java.lang.NullPointerException
08-12 14:44:40.105: ERROR/AndroidRuntime(14525): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
08-12 14:44:40.105: ERROR/AndroidRuntime(14525): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
08-12 14:44:40.105: ERROR/AndroidRuntime(14525): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
08-12 14:44:40.105: ERROR/AndroidRuntime(14525): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
08-12 14:44:40.105: ERROR/AndroidRuntime(14525): at android.os.Handler.dispatchMessage(Handler.java:99)
08-12 14:44:40.105: ERROR/AndroidRuntime(14525): at android.os.Looper.loop(Looper.java:123)
08-12 14:44:40.105: ERROR/AndroidRuntime(14525): at android.app.ActivityThread.main(ActivityThread.java:3683)
08-12 14:44:40.105: ERROR/AndroidRuntime(14525): at java.lang.reflect.Method.invokeNative(Native Method)
08-12 14:44:40.105: ERROR/AndroidRuntime(14525): at java.lang.reflect.Method.invoke(Method.java:507)
08-12 14:44:40.105: ERROR/AndroidRuntime(14525): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-12 14:44:40.105: ERROR/AndroidRuntime(14525): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-12 14:44:40.105: ERROR/AndroidRuntime(14525): at dalvik.system.NativeStart.main(Native Method)
08-12 14:44:40.105: ERROR/AndroidRuntime(14525): Caused by: java.lang.NullPointerException
08-12 14:44:40.105: ERROR/AndroidRuntime(14525): at a.b.MyIncidentActivity.onCreate(MyIncidentActivity.java:50)
08-12 14:44:40.105: ERROR/AndroidRuntime(14525): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-12 14:44:40.105: ERROR/AndroidRuntime(14525): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
08-12 14:44:40.105: ERROR/AndroidRuntime(14525): ... 11 more
有人可以幫我找出原因。我迷失了這個錯誤。在同一應用程序內同樣的實現工作正常,但不知道爲什麼它不工作。
發帖引發NPE的請 – Egor
你有什麼行代碼在線沒有50. – Umesh
什麼是setListAdapter();?並顯示MyIncidentActivity.java第50行 –