Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can someone explain to me this `StaleDataException`

Can someone explain to me this StaleDataException

07-11 19:58:23.298 E/AndroidRuntime( 1044): Uncaught handler: thread main exiting due to uncaught exception
07-11 19:58:23.368 E/AndroidRuntime( 1044): android.database.StaleDataException: Access closed cursor
07-11 19:58:23.368 E/AndroidRuntime( 1044): at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:217)
07-11 19:58:23.368 E/AndroidRuntime( 1044): at android.database.AbstractWindowedCursor.getInt(AbstractWindowedCursor.java:84)
07-11 19:58:23.368 E/AndroidRuntime( 1044): at android.database.CursorWrapper.getInt(CursorWrapper.java:128)

When and how do we need to assure a requiry on the cursor, and why fails with this Exception?

like image 405
Pentium10 Avatar asked Jul 12 '10 20:07

Pentium10


2 Answers

You are trying to retrieve information from a Cursor that has already been closed. You must verify whether the cursor is closed or not by using the isClosed method.

like image 156
Cristian Avatar answered Sep 24 '22 14:09

Cristian


You can't close the cursor until CursorAdapter is no longer needed. So you can close it in onDestroy() method:

@Override
public void onDestroy() {
 super.onDestroy();

     //Close the cursor
     cursor.close();
     //Close the database
     database.close();
    }
like image 45
VenSan Avatar answered Sep 23 '22 14:09

VenSan