安卓:ListView控件崩溃 [英] Android: listview crashes
问题描述
我采用了android ListView控件及其工作完美的罚款。我的实现如下
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));
现在我介绍onItemClickListener下面和应用程序崩溃没有理由。
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的
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.
推荐答案
好吧,你从code,你正在使用
Well from you code where you are using
setListAdapter(adapter);
ListActivity
表明您正在使用,并在使用 ListActivity
你得到你的列表如下:
indicates that you are using ListActivity
, and while using ListActivity
you get your list as follow:
listView = this.getListView();
也可以使用
listView = this.findViewById(android.R.id.list);
和你使用
listView = this.findViewById(R.id.list);
这是错误的 ListActivity
和的ListView
的情况下,无法找到和结果 NullPointerException异常
。
which is wrong in case of ListActivity
, and ListView
cannot be found and results in NullPointerException
.
这篇关于安卓:ListView控件崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!