Custom SimpleCursorAdapter, database query and NullPointerException

前端 未结 1 967
陌清茗
陌清茗 2021-02-06 17:34

I\'m trying to make ListView populated from database, and spice each row with fancy delete button. So I made list Activity and custom SimpleCursorAdapter.

This is main Li

1条回答
  •  后悔当初
    2021-02-06 17:57

    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).

    0 讨论(0)
提交回复
热议问题