Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 626
Marian Klühspies Avatar asked Apr 26 '13 19:04

Marian Klühspies


2 Answers

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?)

like image 152
anddev84 Avatar answered Oct 29 '22 08:10

anddev84


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

like image 40
Asahi Avatar answered Oct 29 '22 08:10

Asahi