java.lang.IllegalStateException: Couldn't read row x col x from CursorWindow.Make sure the Cursor is initialized correctly.contacts
Asked Answered
M

2

6

I´ve developed an application, which goes through all contacts on android. It´s already published and currently installed on about ~800 devices. It is running on almost all devices without any problems, but on some I get the error via BugSense and I have not found a working solution yet.

Here is one of the stacktraces I´m getting:

java.lang.IllegalStateException: Couldn't read row 0, col 8 from CursorWindow. Make sure the Cursor is     initialized correctly before accessing data from it.
at android.os.Parcel.readException(Parcel.java:1335)
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:182)
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:136)
at android.database.BulkCursorProxy.getWindow(BulkCursorNative.java:192)
at android.database.BulkCursorToCursorAdaptor.onMove(BulkCursorToCursorAdaptor.java:94)
at android.database.AbstractCursor.moveToPosition(AbstractCursor.java:178)
at android.database.AbstractCursor.moveToNext(AbstractCursor.java:209)
at android.database.CursorWrapper.moveToNext(CursorWrapper.java:166)
at de.android.contactscleaner.ContactsActivity.deleteContacts(ContactsActivity.java:118)
at de.android.contactscleaner.ContactsActivity$1.run(ContactsActivity.java:61)
at java.lang.Thread.run(Thread.java:856)

In my code, I do the following before I access the cursor, which also is part of a solution:

    private void initCursor() {
    cr = getContentResolver();

    if (cr != null) {
        cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null,
                null, null);

    }
}

private void retryCursorInitialisation() {

    while (attempt < Rules.cursor_init_attempts) {
        attempt++;
        initCursor();
        // Check if cursor is initialisated correctly
        if (cur != null && cur.getColumnCount() != 0) {
            if (attempt >= Rules.cursor_init_attempts_to_report) {
                BugSenseHandler.sendEvent("Cursor init succeded after "
                        + attempt + "/" + Rules.cursor_init_attempts
                        + " retries");
            }
            break;
        } else {
            if (attempt == Rules.cursor_init_attempts) {
                BugSenseHandler
                        .sendEvent("Cursor init completly failed after "
                                + attempt + " attempts");
            }
        }

    }
}

It does never re-initialisate the cursor, if it is "broken", because cur.getColumCount() is never 0.

(I read in another thread on stackoverflow, that you should check if column count is 0 instead of checking if the Cursor is null, but that does not work. That means, there is really only a problem with certain columns/rows.

The part, where the error occurs is a simple

        while (cur.moveToNext())

Edit:

The complete code segment arround the part, where the problem occurs:

        if (cur != null && cur.getColumnCount() != 0) {
        try {
            cur.moveToFirst();
        } catch (Exception e) {
            initCursor();
        }

        while (cur.moveToNext()) ....

Please help me, I´m getting more and more bad ratings without being able to do anything

Missive answered 26/4, 2013 at 19:50 Comment(0)
J
2

It appears this may be related to a bug submitted to Google: http://code.google.com/p/android/issues/detail?id=32472 . It says assigned but I'm not seeing any activity on it since last year.

(Pursuant to the bug, are you updating any rows in the backing DB table that would cause the row count to be different when the CursorWindow is updated?)

Jena answered 22/5, 2014 at 15:40 Comment(0)
R
2

Try positioning cursor using moveToFirst and checking its count to make sure it's not empty before attempting to read any data from it.

Riti answered 26/4, 2013 at 20:3 Comment(6)
ah sry, didn´t mention that. I already do it bevor moving cursor.Just added it to the main postLaquanda
you're still missing cursor count check - the cursor could be empty and therefore will not moveToNextRiti
ahh okay, instead of cursor.getColumCount just cursor.getCount() and thats it? Have you any idea for reproducing this error and checking, if the solution works?Laquanda
make your query return nothing, i.e. empty cursor by either removing all contacts or adding some false conditionRiti
okay thank you, I´ll push an update and hopefully I never see this error again :)Laquanda
The update is out, but i get exactly the same error again, even if I check if the count is greater zero before. But now I catch the error at least, so the app won´t crash. I can´t get it ... :(Laquanda
J
2

It appears this may be related to a bug submitted to Google: http://code.google.com/p/android/issues/detail?id=32472 . It says assigned but I'm not seeing any activity on it since last year.

(Pursuant to the bug, are you updating any rows in the backing DB table that would cause the row count to be different when the CursorWindow is updated?)

Jena answered 22/5, 2014 at 15:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.