android.database.StaleDataException: Attempted to access a cursor after it has been closed
Asked Answered
S

1

6

This is fragment activity it force closed when i press home button, error is Attempted to access a cursor after it has been closed. Where is error. i also try getContentResolver().query() instead of managedQuery() same error.

and my MainActivity is SherlockFragmentActivity tabs and view pager

private Cursor getInternalAudioCursor(String selection,
        String[] selectionArgs) {
    return getActivity().managedQuery(
            MediaStore.Audio.Media.INTERNAL_CONTENT_URI,
            INTERNAL_COLUMNS,
            selection,
            selectionArgs,
            MediaStore.Audio.Media.DEFAULT_SORT_ORDER);
}

private Cursor getExternalAudioCursor(String selection,
        String[] selectionArgs) {
    return getActivity().managedQuery(
            MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
            EXTERNAL_COLUMNS,
            selection,
            selectionArgs,
            MediaStore.Audio.Media.DEFAULT_SORT_ORDER);
}

Cursor createCursor(String filter) {
    ArrayList<String> args = new ArrayList<String>();
    String selection;

    if (mShowAll) {
        selection = "(_DATA LIKE ?)";
        args.add("%");
    } else {
        selection = "(";
        for (String extension : CheapSoundFile.getSupportedExtensions()) {
            args.add("%." + extension);
            if (selection.length() > 1) {
                selection += " OR ";
            }
            selection += "(_DATA LIKE ?)";
            selection  = selection  + "AND (IS_MUSIC=1)";
        }
        selection += ")";

        selection = "(" + selection + ") AND (_DATA NOT LIKE ?)";
        args.add("%espeak-data/scratch%");
    }

    if (filter != null && filter.length() > 0) {
        filter = "%" + filter + "%";
        selection =
            "(" + selection + " AND " +
            "((TITLE LIKE ?) OR (ARTIST LIKE ?) OR (ALBUM LIKE ?)))";
        args.add(filter);
        args.add(filter);
        args.add(filter);
    }

    String[] argsArray = args.toArray(new String[args.size()]);

    getExternalAudioCursor(selection, argsArray);
    getInternalAudioCursor(selection, argsArray);

    Cursor c = new MergeCursor(new Cursor[] {
            getExternalAudioCursor(selection, argsArray),
            getInternalAudioCursor(selection, argsArray)});
    getActivity().startManagingCursor(c);
    return c;
}

Eroor log:

01-13 15:02:16.049: E/AndroidRuntime(12435): java.lang.RuntimeException: Unable to resume activity {com.my.ringtone/com.my.ringtone.MainActivity}: android.database.StaleDataException: Attempted to access a cursor after it has been closed.
01-13 15:02:16.049: E/AndroidRuntime(12435):    at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2946)
01-13 15:02:16.049: E/AndroidRuntime(12435):    at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2975)
01-13 15:02:16.049: E/AndroidRuntime(12435):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1307)
01-13 15:02:16.049: E/AndroidRuntime(12435):    at android.os.Handler.dispatchMessage(Handler.java:102)
01-13 15:02:16.049: E/AndroidRuntime(12435):    at android.os.Looper.loop(Looper.java:157)
01-13 15:02:16.049: E/AndroidRuntime(12435):    at android.app.ActivityThread.main(ActivityThread.java:5356)
01-13 15:02:16.049: E/AndroidRuntime(12435):    at java.lang.reflect.Method.invokeNative(Native Method)
01-13 15:02:16.049: E/AndroidRuntime(12435):    at java.lang.reflect.Method.invoke(Method.java:515)
01-13 15:02:16.049: E/AndroidRuntime(12435):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
01-13 15:02:16.049: E/AndroidRuntime(12435):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
01-13 15:02:16.049: E/AndroidRuntime(12435):    at dalvik.system.NativeStart.main(Native Method)
01-13 15:02:16.049: E/AndroidRuntime(12435): Caused by: android.database.StaleDataException: Attempted to access a cursor after it has been closed.
01-13 15:02:16.049: E/AndroidRuntime(12435):    at android.database.BulkCursorToCursorAdaptor.throwIfCursorIsClosed(BulkCursorToCursorAdaptor.java:64)
Stephniestepladder answered 13/1, 2015 at 9:37 Comment(6)
Closed your Cursor after it's been used.Deduce
After Internal and External Cursor used...Deduce
this error only happens when i press home button for close app.. otherwise working fine...Stephniestepladder
that means you have to close cursor at Activity onPause();Deduce
Like Cursor c=createCursor("test"); c.close();Deduce
thank you it work .. onPause i finish activity. now working fineStephniestepladder
G
1

Please use getActivity().getContentResolver().query(); instead of getActivity().managedQuery();.

Grishilde answered 24/3, 2017 at 7:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.