Nik
Nik

Reputation: 3133

Getting java.lang.IllegalStateException: attempt to re-open an already-closed object: when using cursor in Android

I am using Cursor to get data from DB (it's information about the overlay markers on Map). I am loading a map with help of this data. Interaction with the DB through Cursor is done in a asyncTask.

Now here is the problem I am facing. If I press back button while the cursor is running to load the map (i.e. in middle of loading overlay markers in a while loop) I get this error:

Caused by: java.lang.IllegalStateException: attempt to re-open an already-closed object: android.database.sqlite.SQLiteQuery (mSql = SELECT _id, category, latitude, longitude FROM node) 

Here is the complete trace of it:

12-21 11:11:30.173: E/AndroidRuntime(2824): FATAL EXCEPTION: AsyncTask #5
12-21 11:11:30.173: E/AndroidRuntime(2824): java.lang.RuntimeException: An error occured while executing doInBackground()
12-21 11:11:30.173: E/AndroidRuntime(2824):     at android.os.AsyncTask$3.done(AsyncTask.java:200)
12-21 11:11:30.173: E/AndroidRuntime(2824):     at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
12-21 11:11:30.173: E/AndroidRuntime(2824):     at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
12-21 11:11:30.173: E/AndroidRuntime(2824):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
12-21 11:11:30.173: E/AndroidRuntime(2824):     at  java.util.concurrent.FutureTask.run(FutureTask.java:137)
12-21 11:11:30.173: E/AndroidRuntime(2824):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
12-21 11:11:30.173: E/AndroidRuntime(2824):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
12-21 11:11:30.173: E/AndroidRuntime(2824):     at java.lang.Thread.run(Thread.java:1102)
 12-21 11:11:30.173: E/AndroidRuntime(2824): Caused by: java.lang.IllegalStateException: attempt to re-open an already-closed object: android.database.sqlite.SQLiteQuery (mSql = SELECT _id, category, latitude, longitude FROM node) 
 12-21 11:11:30.173: E/AndroidRuntime(2824):    at android.database.sqlite.SQLiteClosable.acquireReference(SQLiteClosable.java:34)
12-21 11:11:30.173: E/AndroidRuntime(2824):     at android.database.sqlite.SQLiteQuery.fillWindow(SQLiteQuery.java:64)
12-21 11:11:30.173: E/AndroidRuntime(2824):     at android.database.sqlite.SQLiteCursor.fillWindow(SQLiteCursor.java:299)
12-21 11:11:30.173: E/AndroidRuntime(2824):     at android.database.sqlite.SQLiteCursor.onMove(SQLiteCursor.java:271)
12-21 11:11:30.173: E/AndroidRuntime(2824):     at android.database.AbstractCursor.moveToPosition(AbstractCursor.java:188)
12-21 11:11:30.173: E/AndroidRuntime(2824):     at android.database.AbstractCursor.moveToNext(AbstractCursor.java:256)
12-21 11:11:30.173: E/AndroidRuntime(2824):     at org.mid.kew.activities.MapPageActivity$MapLoadingAsyncTask.doInBackground(MapPageActivity.java:632)
12-21 11:11:30.173: E/AndroidRuntime(2824):     at org.mid.kew.activities.MapPageActivity$MapLoadingAsyncTask.doInBackground(MapPageActivity.java:1)
12-21 11:11:30.173: E/AndroidRuntime(2824):     at android.os.AsyncTask$2.call(AsyncTask.java:185)
12-21 11:11:30.173: E/AndroidRuntime(2824):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
12-21 11:11:30.173: E/AndroidRuntime(2824):     ... 4 more

and here is a snapshot of code I am using in asyncTask

Under DoInBackground method

openKewDataBase();
Cursor cursor = getCursorForOverLayIcons();
startManagingCursor(cursor);
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
            ...

    cursor.moveToNext();
}

cursor.close();

Under onPostExecute Method

....

closeKewDataBase();

As per I can trace Its crashing at "cursor.moveToNext();"

Upvotes: 1

Views: 5452

Answers (1)

Graham Borland
Graham Borland

Reputation: 60721

As you are manually closing the cursor with cursor.close(), you shouldn't call startManagingCursor(cursor). You need to choose one or the other.

As you are doing this in an AsyncTask, you almost certainly don't want the enclosing Activity to manage the cursor for you, as the AsyncTask can outlive the Activity. So just do it manually.

(I presume the AsyncTask is an inner class of your Activity.)

Upvotes: 1

Related Questions