Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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)
like image 331
Higginslpert Avatar asked Jan 13 '15 09:01

Higginslpert


1 Answers

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

like image 157
user2281660 Avatar answered Oct 18 '22 18:10

user2281660