1

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();"

halfer
  • 19,824
  • 17
  • 99
  • 186
Nik
  • 2,913
  • 7
  • 40
  • 66

1 Answers1

1

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

Graham Borland
  • 60,055
  • 21
  • 138
  • 179
  • Problem Solved... Thank you Graham Borland – Nik Dec 21 '11 at 12:24
  • Hi Graham i am facing the same problem.. could you check [my post](http://stackoverflow.com/questions/11633581/attempt-to-re-open-an-already-closed-object-java-lang-illegalstateexception/11633761#comment15427898_11633761) please. – vinothp Jul 25 '12 at 08:40