代码之家  ›  专栏  ›  技术社区  ›  Jack Moe

BaseExpandableListAdapter的getChildrenCount()上的NullPointerException

  •  0
  • Jack Moe  · 技术社区  · 8 年前

    这是我的一段代码(重要的是,如果我在数据库中发现另一个模式对象具有相同的父ID,我必须删除旧的):

    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();
    }
    
    2 回复  |  直到 8 年前
        1
  •  1
  •   Jack Moe    8 年前

    好吧,过了一会儿我才意识到我删除的是孩子,而不是相应的家长。因此,getChildCount方法给了我一个错误,因为groupCount值是4,而它必须是3。

    谢谢大家!

        2
  •  0
  •   adickinson    8 年前
    java.lang.NullPointerException: Attempt to invoke virtual method 'int java.util.ArrayList.size()' on a null object reference
    

    再次检查它是否确实已初始化。