I developed an application that goes through all the contacts on Android. It has already been published and is currently installed on approximately ~ 800 devices. It works on almost all devices without any problems, but on some I get an error message through BugSense, and I have not yet found a working solution.
Here is one of the stacks I get:
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 moving on to the cursor, which is also part of the 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 never reinitializes the cursor if it is “broken” because cur.getColumCount () is never 0.
(I read in another stackoverflow that you should check if the column is a number of 0 instead of checking if the cursor is null, but this does not work. This means that there is really only a problem with certain columns / rows.
The part where the error occurs is simple
while (cur.moveToNext())
Edit:
The full code segment around 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 get more and more bad grades, not being able to do anything