首页 > 解决方案 > 在 getChildrenCount(int groupPosition) 上的 ExpandableListView 空指针

问题描述

我有要加载到 ExpandableListView 的对象的 ArrayList。标题已正确加载和显示,但是当我想扩展其中一个标题时,我得到了期望

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

这个异常发生在方法中getChildrenCount(int groupPosition)

当我在 return 上放置断点时,我看到 _listDataChild 和 _listDataHeader 都有数据并且所有内容都已初始化,所以我无法弄清楚为什么会出现空指针异常。

这是代码:

public class ExpandableAdapter extends BaseExpandableListAdapter {

    private Context _context;
    private ArrayList<SPCLogModel> _listDataHeader = new ArrayList<SPCLogModel>();
    private HashMap<ArrayList<SPCLogModel>, ArrayList<SPCLogModel>> _listDataChild = new HashMap<ArrayList<SPCLogModel>, ArrayList<SPCLogModel>>();

    public ExandebleAdapter(Context _context, ArrayList<SPCLogModel> listDataHeader, HashMap<ArrayList<SPCLogModel>, ArrayList<SPCLogModel>> listDataChild) {
        this._context = _context;
        this._listDataHeader = listDataHeader;
        this._listDataChild = listDataChild;
    }

    @Override
    public int getGroupCount() {
        return this._listDataHeader.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return this._listDataChild.get(this._listDataHeader.get(groupPosition)).size();
    }


    @Override
    public Object getGroup(int groupPosition) {
        return this._listDataHeader.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return this._listDataChild.get(this._listDataHeader.get(groupPosition))
                .get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
                             View convertView, ViewGroup parent) {
        //String headerTitle = (String) getGroup(groupPosition);
        SPCLogModel mod = (SPCLogModel) getGroup(groupPosition);

        if (convertView == null) {
            LayoutInflater layoutInflater = (LayoutInflater) this._context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = layoutInflater.inflate(R.layout.list_group, null);
        }
        TextView listTitle = (TextView) convertView
                .findViewById(R.id.tv_listtitle);
        listTitle.setTypeface(null, Typeface.BOLD);
        //listTitle.setText(headerTitle);
        listTitle.setText(mod.getDO_No());
        return convertView;
    }

      @Override
public View getChildView(int groupPosition, final int childPosition,
                         boolean isLastChild, View convertView, ViewGroup parent) {
    //final String childText = (String) getChild(groupPosition, childPosition);
    SPCLogModel mod = (SPCLogModel) getGroup(groupPosition);
    if (convertView == null) {
        LayoutInflater layoutInflater = (LayoutInflater) this._context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = layoutInflater.inflate(R.layout.list_child, null);
    }
    TextView expandedListTextView = (TextView) convertView
            .findViewById(R.id.tv_listchild);
    //expandedListTextView.setText(childText);
    expandedListTextView.setText(mod.getBrojKutije().toString());
    return convertView;
}

    @Override
    public boolean isChildSelectable(int i, int i1) {
        return false;
    }

获取数据

public class asyncTaskGetAllSPC extends AsyncTask<String, String, ArrayList<SPCLogModel>> {

    // For task finish detection
    public interface AsynResponse {
        void processFinish(Boolean output);
        void processFinish(Boolean output, ArrayList<SPCLogModel> listspc, ArrayList<SPCLogModel> listspcgen);

    }

    AsynResponse asynResponse = null;

    final ArrayList<SPCLogModel> itemListSPC = new ArrayList<SPCLogModel>();
    final ArrayList<SPCLogModel> itemListProvjeraGen = new ArrayList<SPCLogModel>();
    private Fragment activityName;

    public asyncTaskGetAllSPC() {
        super();
    }

    public asyncTaskGetAllSPC(GalleryFragment activityName) {
        this.activityName = activityName;
    }

    // For feedback info if task is finished
    public asyncTaskGetAllSPC(Fragment activityName, AsynResponse asynResponse) {
        this.activityName = activityName;
        this.asynResponse = asynResponse;
    }
    @Override
    protected ArrayList<SPCLogModel> doInBackground(String... strings) {
        //return null;

        // return new ProcessData().ProcessOrders();
        String retVal = new ProcessData().ProcessGetRequest(Api.URL_GET_ALLSPC);

        ArrayList<HashMap<String, String>> listaStavki = new ArrayList<HashMap<String, String>>();
        listaStavki = new ProcessData().DecodeSPCAll(retVal);

        for (int i=0; i<=listaStavki.size()-1; i++) {
            SPCLogModel spclogheader = new SPCLogModel(
                    listaStavki.get(i).get("UID"),
                    listaStavki.get(i).get("DatumSPC"),
                    Integer.parseInt(listaStavki.get(i).get("IDKontrolor")),
                    listaStavki.get(i).get("DO_No"),
                    listaStavki.get(i).get("LI_Article"),
                    Integer.parseInt(listaStavki.get(i).get("ProducedQTY"))
            );

            SPCLogModel spcprovjeragen = new SPCLogModel(
                    listaStavki.get(i).get("UIDOrder"),
                    Integer.parseInt(listaStavki.get(i).get("BrojKutije")),
                    Integer.parseInt(listaStavki.get(i).get("BrojKomada")),
                    listaStavki.get(i).get("Napomena")
            );

            itemListSPC.add(spclogheader);
            itemListProvjeraGen.add(spcprovjeragen);
        }

        HashSet<String> seen=new HashSet<>();
        final boolean b = itemListSPC.removeIf(e -> !seen.add(e.getUID()));

        return itemListSPC;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected void onPostExecute(ArrayList<SPCLogModel> items) {
        super.onPostExecute(items);
        asynResponse.processFinish(true, itemListSPC, itemListProvjeraGen);
    }

填充 ExpandableListView

private void LoadData()
    {            
        asyncTaskGetAllSPC t = (asyncTaskGetAllSPC) new asyncTaskGetAllSPC(ListSPC.this, new asyncTaskGetAllSPC.AsynResponse() {
            @Override
            public void processFinish(Boolean output) {

            }

            @Override
            public void processFinish(Boolean output, ArrayList<SPCLogModel> listspc, ArrayList<SPCLogModel> listspcgen) {
                ArrayList<SPCLogModel> itemListSPC = listspc;
                ArrayList<SPCLogModel> itemListProvjeraGen = listspcgen;
                listDataChild = new HashMap<ArrayList<SPCLogModel>, ArrayList<SPCLogModel>>();
                listDataHeader = listspc;
                listDataChild.put(listDataHeader, listspcgen);

                PopulateExList();
            }
        }).execute();
    }
 private void PopulateExList()
    {
        expandableListAdapter = new ExandebleAdapter(ListSPC.this.getActivity(), listDataHeader, listDataChild);
        exp_leaseoffer.setAdapter(expandableListAdapter);
    }

标签: androidexpandablelistview

解决方案


推荐阅读