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

后端 未结 2 1708
攒了一身酷
攒了一身酷 2021-01-14 04:39

I\'m trying to figure out why occasionally I\'m getting the IllegalStateException. I can\'t find any good examples that show how to load a list using a thread to query a SQ

2条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-14 05:00

    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);
            } 
            }
        }
    

提交回复
热议问题