java.lang.IllegalStateException: attempt to re-open an already-closed object

梦想与她 提交于 2019-12-01 09:05:53

Look into AsyncQueryHandler if you want to query DB the way you want.

Your task RetrieveCursorTask is running on separate thread so when your activity gets destroyed your AsyncTask might still be running in background but as you have closed your cursor in main activity onDestroy it might be requeried again after your AsyncTask returns.

Sounds like you need to syncronize the block where you set your adapter in onPostExecute. The problem is since AsyncTask is running on a separate thread, the order in which the cursor is set and subsequently requested isn't guaranteed. Try this..

    @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);
        } 
        }
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!