Custom SimpleCursorAdapter, database query and NullPointerException

梦想的初衷 提交于 2019-12-03 03:55:40

The problem is in your SMSimpleCursorAdapter code:

EditEntries dbDel = new EditEntries(); //from previous code sample

You create a new object but it won't be a managed Activity (eg. the onCreate method won't be called). The NPE probably comes from your DBAdapter when you try to create it the second time (from your Adapter).

Quick fix:

EditEntries dbDel; //from previous code sample
public SMSimpleCursorAdapter(Context context, int layout, Cursor c,
        String[] from, int[] to) {
    super(context, layout, c, from, to);

    this.c = c;
    this.context = context;
    this.activity = (Activity) context;
    this.dbDel = (EditEntries) context;
}

If you don't want just a quick fix the following would be a much better solution:

  1. Move the delRow, getEvents method into your DBAdapter class
  2. Modify the constructor of your SMSimpleCursorAdapter to give in the DBAdapter class (created in your ListActivity).
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!