0

我已被當值「編輯類...」在ListView:的Android的ListView適配器視圖空指針異常

public void editClassOptionPressed(AdapterView<?> parent,int position,ArrayList<Class>classesArrayList){ 
    //The line below throws the null pointer exception 
    parent.getChildAt(position).setBackgroundColor(Color.BLUE); 
    Toast.makeText(getApplicationContext(), "Please Select Class To Edit...",Toast.LENGTH_LONG).show(); 
    setEditListener(classesArrayList, adapter); 
} 

此方法由以下的方法調用被選擇稱爲以下的方法,這又稱爲我的onCreate方法:

public void setDefaultListener(final ArrayList<Class>classesArrayList){ 
    ListView lv = getListView(); 
    lv.setTextFilterEnabled(true); 
    lv.setOnItemClickListener(new OnItemClickListener() { 
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
     if(position==(classesArrayList.size()-3)){ 
      Intent intent = new Intent(ClassView.this, AddNewClassView.class); 
      startActivity(intent); 
     } 
     else if(position==(classesArrayList.size()-1)){ 
      deleteClassOptionPressed(parent,position); 
     } 
     else if(position==(classesArrayList.size()-2)){ 
      **editClassOptionPressed(parent, position, classesArrayList);** 
     } 
     else{ 
      Intent intent = new Intent(ClassView.this, GradeView.class); 
      intent.putExtra("selectedClassName",((TwoLineListItem)view).getText1().getText()); 
      startActivity(intent); 
     } 
     } 
    }); 
} 

從我到目前爲止調試,它看來,家長不那個位置寄存器一個孩子,雖然父母本身就是非空。我還應該指出,只有在ListView中有一定數量的項目時纔會發生這種情況。有人有主意嗎?這裏是logcat的輸出:

06-26 18:31:23.373: E/AndroidRuntime(1243): FATAL EXCEPTION: main 
06-26 18:31:23.373: E/AndroidRuntime(1243): java.lang.NullPointerException 
06-26 18:31:23.373: E/AndroidRuntime(1243): athellolistview.com.ClassView.editClassOptionPressed(ClassView.java:112) 
06-26 18:31:23.373: E/AndroidRuntime(1243):  at hellolistview.com.ClassView$4.onItemClick(ClassView.java:266) 
06-26 18:31:23.373: E/AndroidRuntime(1243):  at android.widget.AdapterView.performItemClick(AdapterView.java:292) 
06-26 18:31:23.373: E/AndroidRuntime(1243):  at android.widget.AbsListView.performItemClick(AbsListView.java:1058) 
06-26 18:31:23.373: E/AndroidRuntime(1243):  at android.widget.AbsListView$PerformClick.run(AbsListView.java:2514) 
06-26 18:31:23.373: E/AndroidRuntime(1243):  at android.widget.AbsListView$1.run(AbsListView.java:3168) 
06-26 18:31:23.373: E/AndroidRuntime(1243):  at android.os.Handler.handleCallback(Handler.java:605) 
06-26 18:31:23.373: E/AndroidRuntime(1243):  at android.os.Handler.dispatchMessage(Handler.java:92) 
06-26 18:31:23.373: E/AndroidRuntime(1243):  at android.os.Looper.loop(Looper.java:137) 
06-26 18:31:23.373: E/AndroidRuntime(1243):  at android.app.ActivityThread.main(ActivityThread.java:4424) 
06-26 18:31:23.373: E/AndroidRuntime(1243):  at java.lang.reflect.Method.invokeNative(Native Method) 
06-26 18:31:23.373: E/AndroidRuntime(1243):  at java.lang.reflect.Method.invoke(Method.java:511) 
06-26 18:31:23.373: E/AndroidRuntime(1243):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 
06-26 18:31:23.373: E/AndroidRuntime(1243):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 
06-26 18:31:23.373: E/AndroidRuntime(1243):  at dalvik.system.NativeStart.main(Native Method) 
+1

每當你的應用程序崩潰,你應該發佈logcat錯誤。使用NPE,logcat會告訴您發生錯誤的確切線...... – Sam

+1

爲什麼不完全避免使用'getChild()',而只是將'View view'直接傳遞給'editClassOptionPressed()'而不是父類和位置? – Geobits

+0

這對我有效,謝謝。但我仍然好奇爲什麼我的方法沒有。 –

回答

0

位置= childPosition

也許你應該考慮使用ExpandableListView,這與這個問題可以幫助:

int childPosition = ExpandableListView.getPackedPositionChild(position);