Reputation: 11
My main Activity (say A) raises intent to fetch a ContactID from ContactsContract through
startActivityForResult(new Intent(Intent.ACTION_PICK, contactsContract.Contacts.CONTENT_URI),PICK_CONTACT);
While returning (onActivityResult(int reqCode, int resultCode, Intent data)), I am using details from 'data' to query the contacts through
if (resultCode == Activity.RESULT_OK) {
Uri contactData = data.getData();
Cursor c = managedQuery(contactData, null, null, null, null);
......
my code works fine with Android 2.x. However this code gets crashed in ICS (Android 4.x), while resuming the main Activity(A). I am not sure, whether it is due to the deprecated 'managedQuery'. Can somebody offer some solution, I am stuck and unable to even find which line causes the error as the Log cat is not showing even the error line number. it simply says Caused by: android.database.StaleDataException: Attempted to access a cursor after it has been closed. Thanks in advance Joshy
I have tried the above code just after completing the requirement of the cursor. Still the problem persists. My Log cat says as follows
08-13 00:26:31.427: W/dalvikvm(3176): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
08-13 00:26:31.447: E/AndroidRuntime(3176): FATAL EXCEPTION: main
08-13 00:26:31.447: E/AndroidRuntime(3176): java.lang.RuntimeException: Unable to resume activity {com.desquare.sp/com.desquare.sp.SmartPadActivity}: android.database.StaleDataException: Attempted to access a cursor after it has been closed.
08-13 00:26:31.447: E/AndroidRuntime(3176): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2444)
08-13 00:26:31.447: E/AndroidRuntime(3176): at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2472)
08-13 00:26:31.447: E/AndroidRuntime(3176): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1173)
08-13 00:26:31.447: E/AndroidRuntime(3176): at android.os.Handler.dispatchMessage(Handler.java:99)
08-13 00:26:31.447: E/AndroidRuntime(3176): at android.os.Looper.loop(Looper.java:137)
08-13 00:26:31.447: E/AndroidRuntime(3176): at android.app.ActivityThread.main(ActivityThread.java:4424)
08-13 00:26:31.447: E/AndroidRuntime(3176): at java.lang.reflect.Method.invokeNative(Native Method)
08-13 00:26:31.447: E/AndroidRuntime(3176): at java.lang.reflect.Method.invoke(Method.java:511)
08-13 00:26:31.447: E/AndroidRuntime(3176): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
08-13 00:26:31.447: E/AndroidRuntime(3176): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
08-13 00:26:31.447: E/AndroidRuntime(3176): at dalvik.system.NativeStart.main(Native Method)
08-13 00:26:31.447: E/AndroidRuntime(3176): Caused by: android.database.StaleDataException: Attempted to access a cursor after it has been closed.
08-13 00:26:31.447: E/AndroidRuntime(3176): at android.database.BulkCursorToCursorAdaptor.throwIfCursorIsClosed(BulkCursorToCursorAdaptor.java:75)
08-13 00:26:31.447: E/AndroidRuntime(3176): at android.database.BulkCursorToCursorAdaptor.requery(BulkCursorToCursorAdaptor.java:144)
08-13 00:26:31.447: E/AndroidRuntime(3176): at android.database.CursorWrapper.requery(CursorWrapper.java:186)
08-13 00:26:31.447: E/AndroidRuntime(3176): at android.app.Activity.performRestart(Activity.java:4505)
08-13 00:26:31.447: E/AndroidRuntime(3176): at android.app.Activity.performResume(Activity.java:4531)
08-13 00:26:31.447: E/AndroidRuntime(3176): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2434)
08-13 00:26:31.447: E/AndroidRuntime(3176): ... 10 more
Upvotes: 1
Views: 1739
Reputation: 87
The API startManagingCursor/stopManagingCursor is deprecated and is the root cause of the issue. For example, please comment this out and verify.
Upvotes: 0
Reputation: 121779
It sounds like this is the problem:
* android.database.staledataexception : Access closed cursor
You seem to be getting the cursors using managedQuery.
I had problems with restoring an application, which for some reason in Icecream Sandwich did not close the cursors properly. The fix was to do this for all cursors right after using them:
if (cursor != null && !cursor.isClosed()) {
myActivity.stopManagingCursor( cursor );
cursor.close();
}
Upvotes: 6