Search code examples
javaandroidsqlitecursorandroid-cursoradapter

Couldn't read row 384, col 47 from CursorWindow. Make sure the Cursor is initialized correctly


I'm reading some data from Android calendar and sometimes I got strange crash reports from users like:

java.lang.IllegalStateException: Couldn't read row 384, col 47 from CursorWindow. 
Make sure the Cursor is initialized correctly before accessing data from it.

My code is here (bold is line where app crashes):

        Cursor eventCursor = contentResolver.query
            (builder.build(), 
            null, 
            CalendarContract.Instances.CALENDAR_ID + " IN (" + ids  + ")", 
            null, 
            null);

        if (eventCursor == null)
            return true;

        while (eventCursor.moveToNext()) {  //this line causecrash
            ... do something...
        }

Why this happens? It can't be simulated. It never ever happened to me and I just can't understand reason nor the error message.


Solution

  • In the beginning of your iteration use eventCursor.moveToFirst() to move to the first row. You may use something like this:

    if (eventCursor != null) {
    
        //Start from beginning
        eventCursor.moveToFirst();
    
        // Loop over rows
        while (eventCursor.moveToNext()) {
    
            // Do Somehing here
        }
     }
    

    You can also check whether the Cursor has rows or not by using eventCursor.getCount().