Java – NullPointerException in expandablelistadapter. Getchild()

Trapped here for more than a week, but it still can't be solved! I have an extensible listview in which data is retrieved from SQLite and set to explistadapter. After clicking the arrow, it will display two children

AddMonthlyExpenses

public class AddMonthlyExpenses extends AppCompatActivity {

    ArrayList<ListObj> groupList= new ArrayList<ListObj>();;
    List<String> childList;
    Map<ListObj, List<String>> laptopCollection;
    ExpandableListView listview;
    expandablelistadapter explistadapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.add_monthly_expenses);
        laptopCollection = new LinkedHashMap<ListObj, List<String>>();
        listview = (ExpandableListView) findViewById(R.id.exlistView);
        explistadapter = new expandablelistadapter(getApplication(), groupList, laptopCollection);
        listview.setAdapter(explistadapter);
        retrieveList(name);
    }

 public void retrieveList(String name) {
        database = mdb.getReadableDatabase();
        Cursor cursor = database.rawQuery("SELECT * FROM " + MyDatabaseHelper.TABLE__TASK + " WHERE Name = ? ", new String[]{name}, null);
        if (cursor != null && cursor.getCount() > 0) {
            while (cursor.moveToNext()) {
                groupList = new ArrayList<ListObj>();
                int iD = cursor.getInt(cursor.getColumnIndex("ID"));
                String month = cursor.getString(cursor.getColumnIndex("Month"));
                double budget = cursor.getDouble(cursor.getColumnIndex("Budget"));
                groupList.add(new ListObj(iD,month,budget));
                if (explistadapter != null) {
                    explistadapter.add(iD, month, budget);
                    createCollection();  // for child items
                    listview.setAdapter(explistadapter);
                }
            }
        }
    }

 private void createCollection() {
        String[] options = {"Edit","Delete"};
        for (ListObj laptop : groupList) {
            loadChild(options);
            laptopCollection.put(laptop, childList);
        }
    }

    private void loadChild(String[] laptopModels) {
        childList = new ArrayList<String>();
        for (String model : laptopModels)
            childList.add(model);
    }
}

expandablelistadapter

  public class expandablelistadapter extends Baseexpandablelistadapter {

    private Context context;
    Map<ListObj, List<String>> laptopCollections;
    private ArrayList<ListObj> laptops;
    double used = 0;
    private LayoutInflater mInflater;

    public expandablelistadapter(Context context, ArrayList<ListObj> laptops, Map<ListObj, List<String>> laptopCollections) {
        this.context = context;
        this.laptopCollections = laptopCollections;
        this.laptops = laptops;
        mInflater = LayoutInflater.from(context);
    }

    public Object getChild(int groupPosition, int childPosition) {  // error line
        if (laptopCollections.get(laptops.get(groupPosition)).get(childPosition) != null && !laptopCollections.get(laptops.get(groupPosition)).get(childPosition).isEmpty()) {
            return laptopCollections.get(laptops.get(groupPosition)).get(childPosition);
        }
        return 1;
    }

    public void add(int id, String month, double budget) {
        String[] splited = month.split("\\s+");
        ListObj obj = new ListObj(id, month, budget);
        obj.setYear(splited[1]);
        obj.setMonth(splited[0]);
        obj.setBudget(budget);
        obj.setID(id);
        laptops.add(obj);
        this.notifyDataSetChanged();
    }

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

    public int getCount() {
        return laptops.size();
    }

    public ListObj getItem(int position) {
        return laptops.get(position);
    }

    public View getChildView(final int groupPosition, final int childPosition,
                             boolean isLastChild, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        final String laptop = (String) getChild(groupPosition, childPosition);   // here the error line 
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.child_item, null);
        }
        TextView edit = (TextView) convertView.findViewById(R.id.textEdit);
        edit.setText(laptop);
        return convertView;
    }

    public int getChildrenCount(int groupPosition) {
        if (laptopCollections.get(laptops.get(groupPosition)) != null && !  laptopCollections.get(laptops.get(groupPosition)).isEmpty()) {
            return  laptopCollections.get(laptops.get(groupPosition)).size();
        }
        return 1;
    }

    public Object getGroup(int groupPosition) {
        return laptops.get(groupPosition);
    }

    public int getGroupCount() {
        return this.laptops.size();
    }

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

    public View getGroupView(int groupPosition, boolean isExpanded,
                             View convertView, ViewGroup parent) {
        ExpensesAdapter.ViewHolder holder = null;

        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.expenses_adapter, null);
            holder = new ExpensesAdapter.ViewHolder();
            holder.month = (TextView) convertView.findViewById(R.id.textMonth);
            holder.budget = (TextView) convertView.findViewById(R.id.textAmount);
            holder.year = (TextView) convertView.findViewById(R.id.textYear);
            convertView.setTag(holder);
        } else {
            holder = (ExpensesAdapter.ViewHolder) convertView.getTag();
        }
        holder.month.setText(laptops.get(groupPosition).getMonth());
        holder.budget.setText(String.format("%.2f", laptops.get(groupPosition).getBudget()));
        holder.year.setText(laptops.get(groupPosition).getYear());
        return convertView;
    }

    public boolean hasStableIds() {
        return true;
    }

    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }
}

error

Screenshot of my app

I tried debugging, but nothing is empty!

You can clone my project from the link below

https://github.com/wseng92/MonthlyExpenses

resolvent:

Add createcollection(); To button1 setonclick listner

createCollection();

groupList = new ArrayList(); May there be a problem with the loop written in it? It creates a new ArrayList for each loop, so your arraysize is always 1 (last iteration count)

while (cursor.moveToNext()) {
                groupList = new ArrayList<ListObj>();
                int iD = cursor.getInt(cursor.getColumnIndex("ID"));
                String month = cursor.getString(cursor.getColumnIndex("Month"));
                double budget = cursor.getDouble(cursor.getColumnIndex("Budget"));
                groupList.add(new ListObj(iD,month,budget));
                createCollection();  // for child items
                if (explistadapter != null) {
                    explistadapter.add(iD, month, budget);
                    listview.setAdapter(explistadapter);
                }
            }

Change to

groupList = new ArrayList<ListObj>();
while (cursor.moveToNext()) {

                int iD = cursor.getInt(cursor.getColumnIndex("ID"));
                String month = cursor.getString(cursor.getColumnIndex("Month"));
                double budget = cursor.getDouble(cursor.getColumnIndex("Budget"));
                groupList.add(new ListObj(iD,month,budget));
                createCollection();  // for child items
                if (explistadapter != null) {
                    explistadapter.add(iD, month, budget);
                    listview.setAdapter(explistadapter);
                }
            }

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>