簡體   English   中英

BaseExpandableListAdapter的getChildrenCount()上的NullPointerException

[英]NullPointerException on getChildrenCount() of BaseExpandableListAdapter

我正在使用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();
}

好的,過了一會兒我才意識到我要刪除孩子,而不是刪除孩子的相應父母。 因此,getChildCount方法給我一個錯誤,因為groupCount值必須為3時為4。

謝謝你們!

java.lang.NullPointerException: Attempt to invoke virtual method 'int java.util.ArrayList.size()' on a null object reference

這告訴您size()方法是NPE的重要原因,因此很有可能是列表為null。

仔細檢查它是否確實已初始化。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM