What can cause StaleDataException other than prematurely calling cursor.close()?
B

2

12

I'm currently heavily modifying/rewriting an Android app and I have seen a very occasional crash along the following lines: a CursorAdapter method is called, it calls AbstractWindowedCursor#checkPosition(), and:

02-20 15:03:18.180 E/AndroidRuntime(17143): android.database.StaleDataException: Attempting to access a closed CursorWindow.Most probable cause: cursor is deactivated prior to calling this method.
02-20 15:03:18.180 E/AndroidRuntime(17143): at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:139)
02-20 15:03:18.180 E/AndroidRuntime(17143): at android.database.AbstractWindowedCursor.getLong(AbstractWindowedCursor.java:74)
02-20 15:03:18.180 E/AndroidRuntime(17143): at android.database.CursorWrapper.getLong(CursorWrapper.java:106)
02-20 15:03:18.180 E/AndroidRuntime(17143): at android.widget.CursorAdapter.getItemId(CursorAdapter.java:220)

The trouble is, we're not closing any Cursors. All our Cursors come from CursorLoaders and in turn are produced by a ContentProvider. We're passing the Cursor into each respective CursorAdapter from the LoaderCallbacks, we're registering the Cursor for notifications in the ContentProvider, we're notifying the ContentResolver from each insert(...), delete(...) and update(...) ... in short I can't find any reason why a Cursor would close while in use.

So: what are the other causes of a StaleDataException?

Beefwitted answered 20/2, 2013 at 15:12 Comment(3)
It's been a while since you asked, but... was there a FilterQueryProvider involved, by any chance?Sanction
Hey Andrew! Were you able to figure out the solution ?Spruce
I don't think we ever definitively tracked this down. However, we did discover that using a Cursor from outside the adapter provoked this behaviour, even though (again) we never called .close() or any method that you'd think caused this. Since then, I've always implemented ContentObserver and DataSetObserver in a manner analogous to android.googlesource.com/platform/frameworks/base/+/master/core/… (skip to the end) if I ever had to use a Cursor both inside and outside an adapter.Beefwitted
D
1

If you are calling Cursor.changeCursor(newCursor), that will automatically close the old cursor for you. This can cause android.database.StaleDataException if anything is still trying to access the cursor.

Deepfreeze answered 6/1, 2019 at 1:40 Comment(0)
R
-3

if you called Context.managedQuery() in android 4.0 and above, you should not call Cursor.close(), if you do so, StaleDataException will be thrown, you can modify you code as follow:

if(VERSION.SDK_INT < 14) {  
    cursor.close();  
}
Reddy answered 18/5, 2014 at 11:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.