Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Honeycomb and cursors (frustrated)

I have this activity that was completely converted to use honeycomb. I reworked all the cursor management to use the new cursorLoader. Everything works great on all other os versions (using the compatibility library), but still does not work on honeycomb. The following stack trace is extremely difficult to understand because it does not tell me which cursor or which line is failing. Furthermore, when debugging, no line of code in ParentActivity is ever executed. This error is happening when I resume the ParentActivity. Works great when I start the activity but fails when returning to it.

05-29 17:23:32.978: ERROR/AndroidRuntime(31692): FATAL EXCEPTION: main
05-29 17:23:32.978: ERROR/AndroidRuntime(31692): java.lang.RuntimeException: Unable to resume activity {com.xxx.xxx/com.xxx.xxx.ParentActivity}: java.lang.IllegalStateException: trying to requery an already closed cursor
05-29 17:23:32.978: ERROR/AndroidRuntime(31692):     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2227)
05-29 17:23:32.978: ERROR/AndroidRuntime(31692):     at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2255)
05-29 17:23:32.978: ERROR/AndroidRuntime(31692):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1028)
05-29 17:23:32.978: ERROR/AndroidRuntime(31692):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-29 17:23:32.978: ERROR/AndroidRuntime(31692):     at android.os.Looper.loop(Looper.java:132)
05-29 17:23:32.978: ERROR/AndroidRuntime(31692):     at android.app.ActivityThread.main(ActivityThread.java:4025)
05-29 17:23:32.978: ERROR/AndroidRuntime(31692):     at java.lang.reflect.Method.invokeNative(Native Method)
05-29 17:23:32.978: ERROR/AndroidRuntime(31692):     at java.lang.reflect.Method.invoke(Method.java:491)
05-29 17:23:32.978: ERROR/AndroidRuntime(31692):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
05-29 17:23:32.978: ERROR/AndroidRuntime(31692):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
05-29 17:23:32.978: ERROR/AndroidRuntime(31692):     at dalvik.system.NativeStart.main(Native Method)
05-29 17:23:32.978: ERROR/AndroidRuntime(31692): Caused by: java.lang.IllegalStateException: trying to requery an already closed cursor
05-29 17:23:32.978: ERROR/AndroidRuntime(31692):     at android.app.Activity.performRestart(Activity.java:4394)
05-29 17:23:32.978: ERROR/AndroidRuntime(31692):     at android.app.Activity.performResume(Activity.java:4420)
05-29 17:23:32.978: ERROR/AndroidRuntime(31692):     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2217)
05-29 17:23:32.978: ERROR/AndroidRuntime(31692):     ... 10 more
like image 963
Michael Little Avatar asked Dec 02 '22 02:12

Michael Little


2 Answers

I ran into the same problem. Here is what I have done. In all the activities that I've called startManagingCursor, I override it as:

@Override
public void startManagingCursor(Cursor c) {

    // To solve the following error for honeycomb:
    // java.lang.RuntimeException: Unable to resume activity 
    // java.lang.IllegalStateException: trying to requery an already closed cursor
    if (Build.VERSION.SDK_INT < VersionUtil.HONEYCOMB) {
        super.startManagingCursor(c);
    }
}

This makes my app run correctly on Honeycomb and earlier versions.

like image 180
davidk Avatar answered Dec 03 '22 14:12

davidk


I had the same problems and think I might have found the problem and a "clean" fix. (I think stopping to manage the cursors at all is not a good idea)

I use Adapter.changeCursor in some activities which worked great in all versions except for honeycomb. The changeCursor method closes the old cursor, but obviously don't stop managing that cursor. So the activity keeps managing that old cursor which is already closed. In a restart of the activity it does try to requery it, without checking if it is closed.

So my solution is:

Cursor oldCursor = mAdapter.getCursor();
mAdapter.changeCursor(newCursor);
stopManagingCursor(oldCursor);

Until now I can't reproduce the Exception again.

like image 36
Maniac Avatar answered Dec 03 '22 15:12

Maniac