Cursor window could not be created from binder

villager picture villager · Jan 14, 2013 · Viewed 12.8k times · Source
1 Cursor cursor = contentResolver.query(MY_URI, new String[] { "first" }, null, null, null);
2 if (cursor != null) {
3   if (cursor.moveToFirst()) {
4       first = cursor.getString(cursor.getColumnIndex("first"));
5       cursor.close();
6   }
7 }

Then on line #3 (according to the logs), I every now and then I come across this exception (excerpt below):

android.database.CursorWindowAllocationException: Cursor window could not be created from binder.
    at android.database.CursorWindow.<init>(CursorWindow.java:134)
    at android.database.CursorWindow.<init>(CursorWindow.java:41)
    at android.database.CursorWindow$1.createFromParcel(CursorWindow.java:709)
    at android.database.CursorWindow$1.createFromParcel(CursorWindow.java:707)
    at android.database.CursorWindow.newFromParcel(CursorWindow.java:718)
    at android.database.BulkCursorProxy.getWindow(BulkCursorNative.java:196)

...

Any ideas why it is throwing this exception? Thanks!

Answer

Justin Breitfeller picture Justin Breitfeller · Jan 23, 2013

I suspect the error may be related to you not closing your cursors properly all the time. Try:

Cursor cursor = contentResolver.query(MY_URI, new String[] { "first" }, null, null, null);
if (cursor != null) {
  if (cursor.moveToFirst()) {
      first = cursor.getString(cursor.getColumnIndex("first"));
  }
  cursor.close(); ///// Changed here
}

The cursor should always be closed (regardless of whether or not its empty). Make sure the rest of your app is doing this as well.