Friday, September 23, 2011

IllegalStateException: Couldn't init cursor window

Today one of our application started getting this exception.
java.lang.IllegalStateException: Couldn't init cursor window at android.database.CursorWindow.native_init(Native Method) at android.database.CursorWindow.(CursorWindow.java:41) at android.database.sqlite.SQLiteCursor.fillWindow(SQLiteCursor.java:277) at android.database.sqlite.SQLiteCursor.getCount(SQLiteCursor.java:269)
After few hours of googling, I found the solution to this problem. Solution is to close the Cursor after after you are done!This exception is thrown here
if (!window->initBuffer(localOnly)) { jniThrowException(env, "java/lang/IllegalStateException", "Couldn't init cursor window"); delete window; return; }
localOnly is defined in
CursorWindow.h
as
#define MAX_WINDOW_SIZE (1024 * 1024)
So, There are two possible causes to throw this exception. 1. Your mobile phone is out of memory.2. You are fetching something out of the database more than 1M. If there is a logical way to divide your queries into discrete chunks, you could do incremental queries and use CursorJoiner to stitch them together that might help you if you are dealing with big chuck of data.

So lesson learned, Close the cursor in a try finally block

Cursor cursor;
try {
cursor.open();
}
finally{
if(cursor.isOpen()) {
cursor.close();
}

2 comments:

  1. Nice post
    but this didn't resolved my issue.
    Still i am getting the error message as
    "java.lang.IllegalStateException: Couldn''t init cursor window"
    Is there another way to resolve this?

    ReplyDelete
  2. Limited file is no more than 1Mb in database?, i have problem is query a file 2mb from dstabse

    ReplyDelete