Android: listview crashes
-
21-12-2020 - |
Question
I'm using android listview and its working perfectly fine. my implementation as below
ListView listview = (ListView)findViewById(R.id.list);
setListAdapter(new ArrayAdapter<String>(MyIncidentActivity.this,
R.layout.row_incident, R.id.label_incident, db_results));
Now I introduced onItemClickListener as below and application crashes for no reason.
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
Can someone help me find out the reason. I'm lost with this error. within same application same implementation works fine but don't know why its not working.
La solution
Well from you code where you are using
setListAdapter(adapter);
indicates that you are using ListActivity
, and while using ListActivity
you get your list as follow:
listView = this.getListView();
or you can use
listView = this.findViewById(android.R.id.list);
and you are using
listView = this.findViewById(R.id.list);
which is wrong in case of ListActivity
, and ListView
cannot be found and results in NullPointerException
.
Autres conseils
Try like this.
Public class ABCD implements OnItemClickListener{
ListView listview;//class varible
listview = (ListView)findViewById(R.id.list);
setListAdapter(new ArrayAdapter<String>(MyIncidentActivity.this,
R.layout.row_incident, R.id.label_incident, db_results));
listview.setOnItemClickListener(this);
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
}
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow