6

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)
General Grievance
  • 4,555
  • 31
  • 31
  • 45
Timbermar
  • 414
  • 7
  • 20
  • 1
    @Timbermar: The only place `SQLiteCursor` throws `IllegalStateException` has nothing to do with `finalize()`, based on the source code, so it is difficult to give you advice. Use `StrictMode` in an Android 2.3 environment to perhaps help you debug this. In the future, consider including the relevant portion of the stack trace in your questions. – CommonsWare Feb 05 '11 at 16:20
  • @CommonsWare: I only mentioned it as to indicate that it wasn't an issue running the queries, it seems to be caused by my cursors not be correctly disposed of. Also, I forgot to mention I use `startManagingCursor` as well, when I create the cursors. Also, I added the stack, all 3 lines of it. This error is not occurring in the main thread, but a separate thread so the app continues to run even as this crashes. – Timbermar Feb 05 '11 at 22:28
  • 1
    @Timbermar: That is not a stack trace. That is Eclipse telling you it intercepted an exception. Let processing continue, and you will get a stack trace in the LogCat pane of the DDMS perspective. – CommonsWare Feb 05 '11 at 22:36
  • @CommonsWare: Stack trace is pretty similar from Logcat, it never makes it into a full blown error or shut down of app. I've added it to the question. – Timbermar Feb 05 '11 at 23:01
  • 3
    @Timbermar: All I can tell you is, that exception does not lie. Somewhere, you missed a `Cursor`. – CommonsWare Feb 05 '11 at 23:16
  • @CommonsWare: I agree, your right, I've just been looking at this for way to long. I totally forgot about **two** other cursors I create (in order to set the spinners to the correct position [shown here](http://stackoverflow.com/questions/2559611/setselection-on-spinner-based-on-rowid/2560990#2560990)) that were buried deep in the onCreate. Thanks for your help. As soon as you mentioned it I went looking for them. I think this is why I should stop coding on Saturdays. Thanks again. I wish you'd made that an answer, so I could have selected it. – Timbermar Feb 05 '11 at 23:27

1 Answers1

16

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

Matt Connolly
  • 9,757
  • 2
  • 65
  • 61