5

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?

Pentium10
  • 204,586
  • 122
  • 423
  • 502

4 Answers4

4

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.

Cristian
  • 198,401
  • 62
  • 356
  • 264
  • Can you please suggest me decent a way of fixing this issue. Currently am getting lot of staleDataException which i couldn't fix it :( – Bytecode Apr 23 '14 at 06:49
1

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();
    }
VenSan
  • 459
  • 3
  • 10
0

In my case, I was closing the cursor in the onStop() method. It turned out that the rotation of the screen was causing this code to run and hence give the StaleDataException.

Hashim Akhtar
  • 813
  • 2
  • 11
  • 16
0

Use Activity.getContentResolver.query() instead of Activity.managedQuery(). Because managedQuery() is deprecated. It works for me.

Manfred Radlwimmer
  • 13,257
  • 13
  • 53
  • 62