2011-08-12 41 views
0

我正在使用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 

有人可以幫我找出原因。我迷失了這個錯誤。在同一應用程序內同樣的實現工作正常,但不知道爲什麼它不工作。

+0

發帖引發NPE的請 – Egor

+0

你有什麼行代碼在線沒有50. – Umesh

+0

什麼是setListAdapter();?並顯示MyIncidentActivity.java第50行 –

回答

3
從你的代碼

好吧,你正在使用

setListAdapter(adapter); 

表明您正在使用ListActivity,並且同時使用ListActivity你得到你的列表如下:

listView = this.getListView(); 

,或者你可以使用

listView = this.findViewById(android.R.id.list); 

和你正在使用

listView = this.findViewById(R.id.list); 

這是錯誤在ListActivity情況下,和ListView不能NullPointerException被發現和結果。

+0

aaaaaaaaaaa ..謝謝goddddd :)你救了我的一天..我多麼傻:) –

+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 

    } 

} 
+0

它也沒有幫助,我也試過它:( –