java.lang.IllegalStateException:尝试重新打开一个已经关闭的对象

| 我试图弄清楚为什么有时会收到IllegalStateException。我找不到任何好的示例来说明如何使用线程来查询SQLite数据库来加载列表。我在下面包含了我的代码。在大多数情况下,它可以正常运行,但是偶尔,我会收到IllegalStateException。 我的另一个活动(也是ExpandableListActivity的一个实例)也得到了类似的例外。该异常指出“试图重新查询已经关闭的游标”。 有人可以告诉我正确的方法来做到这一点,以免引起任何错误吗?我宁愿使用游标,而不是将所有数据复制到内存中。如果无法弄清楚,则必须将其全部加载到内存中。 我认为此问题与startManagingCursor(Cursor)和onDestroy()中的数据库连接已关闭有关。 请帮助 -故事
public class MyListActivity extends ListActivity {
    private MyCursorAdapter adapter;
    private SQLiteDatabase db = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        try {
            new RetrieveCursorTask(this).execute((Void[]) null);
        } catch (Exception e) {
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        // Null out the cursor. 
        if (adapter != null) {
            adapter.changeCursor(null);
            adapter = null;
        }

        if (db != null && db.isOpen()) {
            db.close();
        }
    }

    private class RetrieveCursorTask extends AsyncTask<Void, Void, Cursor> {
        private Context ctx;

        public RetrieveCursorTask(Context ctx) {
            this.ctx = ctx;
        }

        @Override
        protected Cursor doInBackground(Void... params) {
            Cursor cursor = null;
            DbHelper helper = new DbHelper(ctx);

            try {
                db = helper.getReadableDatabase();
                cursor = db.query(\"users\",
                    new String[] {
                        DbHelper.ID_COLUMN,
                        DbHelper.UID_COLUMN
                    }, 
                    null, null, null, null, null);
                startManagingCursor(cursor);
            } catch (Exception e) {
            }
            return cursor;
        }

        @Override
        protected void onPostExecute(Cursor cursor) {
            super.onPostExecute(cursor);

            if (cursor != null) {
                try {
                    adapter = new MyCursorAdapter(ctx, cursor);
                } catch (Exception e) {
                }
                setListAdapter(adapter);
            } 
        }
    }

    private class MyCursorAdapter extends CursorAdapter {
        private Context ctx;

        public MyCursorAdapter(Context context, Cursor c) {
            super(context, c);
            this.ctx = context;
        }

        @Override
        public void bindView(View view, Context context, Cursor cursor) {
            // ...
        }

        @Override
        public View newView(Context context, Cursor cursor, ViewGroup parent) {
            // ...
        }
    }
}
    
已邀请:
        如果要以所需方式查询数据库,请查看AsyncQueryHandler。 您的任务RetrieveCursorTask在单独的线程上运行,因此当您的活动被破坏时,AsyncTask可能仍在后台运行,但是由于您关闭了主活动onDestroy中的光标,因此AsyncTask返回后可能会再次查询它。     
        听起来您需要同步在onPostExecute中设置适配器的块。问题在于,由于AsyncTask在单独的线程上运行,因此不能保证设置游标并随后对其进行请求的顺序。尝试这个..
    @Override
    protected void onPostExecute(Cursor cursor) {
        super.onPostExecute(cursor);

        synchronized(anyObject) {
        if (cursor != null) {
            try {
                adapter = new MyCursorAdapter(ctx, cursor);
            } catch (Exception e) {
            }
            setListAdapter(adapter);
        } 
        }
    }
    

要回复问题请先登录注册