首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NullPointerException at ExpandableListAdapter.getChild()

NullPointerException at ExpandableListAdapter.getChild()
EN

Stack Overflow用户
提问于 2017-10-23 17:14:25
回答 3查看 607关注 0票数 4

被困在这里一个多星期,但仍然无法解决!我有一个expandable listView,其中从SQLite检索数据并设置为expListAdapter。一旦箭头单击,它将显示两个子项。

AddMonthlyExpenses

代码语言:javascript
复制
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

代码语言:javascript
复制
  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;
    }
}

误差

10-26 00:03:57.114 23612-23612/com.example.tony.Monthly管理费E/AndroidRuntime:致命异常:主java.lang.NullPointerException在com.example.tony.monthlyexpenses.adapter.ExpandableListAdapter.getChild(ExpandableListAdapter.java:42) at com.example.tony.monthlyexpenses.adapter.ExpandableListAdapter.getChildView(ExpandableListAdapter.java:87)在android.widget.ExpandableListConnector.getView(ExpandableListConnector.java:451) at android.widget.AbsListView.obtainView(AbsListView.java:2232)在android.widget.ListView.makeAndAddView(ListView.java:1849) at android.widget.ListView.fillDown(ListView.java:678)在android.widget.ListView.fillSpecific(ListView.java:1339)

我的应用程序屏幕截图

我试过调试,但是没有什么是空的!

您可以从下面的链接复制我的项目。

https://github.com/wseng92/MonthlyExpenses

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2017-10-23 17:47:50

添加createCollection();到button1 seton单击listner

代码语言:javascript
复制
createCollection();

groupList =新的ArrayList();在for循环中编写可能会有问题吗?它为每个循环创建一个新的数组列表,因此您的数组大小总是1(最后一次迭代计数)。

代码语言:javascript
复制
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);
                }
            }

更改为

代码语言:javascript
复制
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);
                }
            }
票数 2
EN

Stack Overflow用户

发布于 2017-10-30 22:16:07

几年前我遇到了一个类似的问题。再也记不起是哪个版本了。

从呼叫开始

代码语言:javascript
复制
public Object getChild(int groupPosition, int childPosition) {
   if (laptopCollections.get(laptops.get(groupPosition)).get(childPosition) ...

笔记本电脑的价值是什么?它由构造函数初始化。

代码语言:javascript
复制
public ExpandableListAdapter(Context context, ArrayList<ListObj> laptops ...

对于第二个参数,它是在AddMonthlyExpenses.onCreate()上创建的

代码语言:javascript
复制
expListAdapter =
   new ExpandableListAdapter(getApplication(), groupList, laptopCollection);

并且最初在声明中初始化

代码语言:javascript
复制
 ArrayList<ListObj> groupList= new ArrayList<ListObj>();;

由于一些我不知道的原因-- Android特性--在我的例子中,这个groupList (或类似的成员字段)是NULL,即使它是在声明时初始化的。

但是将初始化移到onCreate()内部就解决了这个问题。

试试看。或者至少调试引用laptops的行。

票数 1
EN

Stack Overflow用户

发布于 2017-10-24 17:05:47

我认为在你的代码中这不起作用

laptopCollections.get(laptops.get(groupPosition)).size();

因为尝试查找其他对象实例。

您的片段和适配器中有这么多列表,您真的不需要这样做。尝试保持OOP并重构它,并为此创建更好的模型。

或者更简单的方法,不使用映射键对象,而是id。

Map<ListObj, List<String>> laptopCollections改为Map<String,List<String>> laptopCollections,其中第一个K是id。也许会有帮助。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46894960

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档