Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent IllegalStateException in SQLiteCursor for Android

I have an ListActivity that is bound to a cursor, when items in the ListView are selected an EditItem activity is launched by startActivityForResult, this EditItem activity preforms several queries, each placed in their own separate Cursor. These are used to populate spinners, much like a lookup field in an Access DB.

My issue is that once the user leaves this EditItem activity, either via submit, cancel or back button, goes back to the ListView Activity and selects another entry in the ListView (same item or a different one) I get IllegalStateException errors in my SQLiteCursor class (Android's, not mine). I am closing my cursors in the onDestroy method of both activities, since sometimes calling for a result will still destroy the calling activity.

This does not always occur on the second selection of an Item, sometimes it will occur on the third selection. I thought maybe I was just moving faster than the OS, so I starting pausing, up to 30 seconds, between my actions, the error is only thrown after calling the activity for result a second or third time. No amount of pausing fixes this.

Edit: The error is in the SQLiteCursor finalize method at the call to super.finalize();

Edit #2: Stack trace for thread:

Daemon System Thread [<5> HeapWorker] (Suspended (exception IllegalStateException)) 
SQLiteCursor.finalize() line: 603   
NativeStart.run() line: not available [native method]   

Edit #3 Stack trace from LogCat (partslist is the table name):

INFO/dalvikvm(599): Ljava/lang/IllegalStateException;: Finalizing cursor android.database.sqlite.SQLiteCursor@437541a0 on partslist that has not been deactivated or closed
INFO/dalvikvm(599):     at android.database.sqlite.SQLiteCursor.finalize(SQLiteCursor.java:596)
INFO/dalvikvm(599):     at dalvik.system.NativeStart.run(Native Method)
like image 511
Timbermar Avatar asked Feb 05 '11 15:02

Timbermar


1 Answers

Make sure you cursor.close() when finished with them.

like image 191
Matt Connolly Avatar answered Oct 17 '22 15:10

Matt Connolly