我有以下方法,当在 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)