我正在使用BaseExpandableListAdapter并尝试做一个动态列表,当您选中或取消选中其他元素时,元素会出现或消失。该组是我称为“调用模式”的对象的ArrayList,子级是这样的HashMap:HashMap>子级。
当我实现要添加子项的列表并且它们被完美添加时,问题就来了,但是当我检查其他元素并且新的子项必须消失时,程序在getChildrenCount()方法中给了我NullPointerException。在添加和删除时,我实际上在两种情况下都使用notifyDataSetChanged()。
这是我的一部分代码(重要的部分是,如果我在数据库中找到另一个具有相同父ID的Mode对象,则必须删除旧的对象):
cbMode.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean isSelected = modeDao.rbIsAlreadySelected(currentChild);
if(!isSelected) {
//if checked uncheck all the children of the same parent AND delete the children
Mode oldMode = modeDao.isThereAnotherRB(currentChild.getIdFather());
if(oldMode != null){
uncheckTheOldCB(oldMode);
setFalseOnCb(oldMode);
modeDao.deleteMode(oldMode);
deleteModeChildren(oldMode);
}
//AND insert it on the DB
modeDao.setSelectedMode(currentChild);
currentChild.setChecked(true);
//if has children show them
if(currentChild.hasChildren() == 1){
setNewFatherMode(currentChild);
}
}else {
//if unchecked delete this from the DB
modeDao.deleteMode(currentChild);
deleteModeChildren(currentChild);
currentChild.setChecked(false);
}
notifyDataSetChanged();
}
});
它显示了我如何删除子代:
private void deleteModeChildren(Mode oldMode){
for (Map.Entry<Mode, ArrayList<Mode>> entry : children.entrySet()) {
Mode currentMode = entry.getKey();
if(currentMode.getId() == oldMode.getId()) {
children.remove(entry.getKey());
break;
}
}
}
这就是我重写getChild方法的方式:
@Override
public Object getChild(int groupPosition, int childPosition) {
return this.children.get(this.parents.get(groupPosition))
.get(childPosition);
}
这是异常的跟踪:
java.lang.NullPointerException: Attempt to invoke virtual method 'int java.util.ArrayList.size()' on a null object reference
at com.iurban.iurbanapp.Adapters.NewModesAdapter.getChildrenCount(NewModesAdapter.java:63)
at android.widget.ExpandableListConnector.refreshExpGroupMetadataList(ExpandableListConnector.java:563)
at android.widget.ExpandableListConnector.access$000(ExpandableListConnector.java:50)
at android.widget.ExpandableListConnector$MyDataSetObserver.onChanged(ExpandableListConnector.java:857)
at android.database.DataSetObservable.notifyChanged(DataSetObservable.java:37)
at android.widget.BaseExpandableListAdapter.notifyDataSetChanged(BaseExpandableListAdapter.java:56)
at com.iurban.iurbanapp.Adapters.NewModesAdapter$3.onClick(NewModesAdapter.java:223)
at android.view.View.performClick(View.java:4787)
at android.widget.CompoundButton.performClick(CompoundButton.java:120)
at android.view.View$PerformClick.run(View.java:19873)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
这是getChildCount方法:
@Override
public int getChildrenCount(int groupPosition) {
return children.get(parents.get(groupPosition)).size();
}
答案 0 :(得分:1)
好吧,过了一会儿,我才意识到我要删除孩子,但不删除孩子的相应父母。因此,getChildCount方法给我一个错误,因为groupCount的值必须为3时为4。
谢谢大家!
答案 1 :(得分:0)
java.lang.NullPointerException: Attempt to invoke virtual method 'int java.util.ArrayList.size()' on a null object reference
这告诉您,size()方法是NPE的基础,因此很有可能是列表为空。
再次检查它是否确实已初始化。