我正在使用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
有人可以帮我找出原因。我迷失了这个错误。在同一个应用程序内同样的实现工作正常,但不知道为什么它不工作。
答案 0 :(得分:4)
您使用的代码
setListAdapter(adapter);
表示您使用的是ListActivity
,在使用ListActivity
时,您的列表如下:
listView = this.getListView();
或者您可以使用
listView = this.findViewById(android.R.id.list);
你正在使用
listView = this.findViewById(R.id.list);
如果ListActivity
出现错误,则无法找到ListView
并导致NullPointerException
。
答案 1 :(得分:0)
试试这样。
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
}
}