20
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!

villager
  • 5,041
  • 11
  • 45
  • 101
  • try this [link](http://stackoverflow.com/questions/5288384/android-content-resolver-query-returning-0-rows-when-it-ought-not-too) – Bhavesh Patadiya Jan 18 '13 at 11:10
  • How many rows are in the database that is backing the `ContentResolver`? That exception is thrown if the cursor fails to allocate enough space to store the data it needs. Since you are not limiting the number of rows in your query via the selection args, your `cursor` will allocate space to keep a copy of all of column `first` in memory. If there are a large number of rows, potentially the process could fail to allocate that memory. – iagreen Jan 21 '13 at 02:46
  • @iagreen, there's only one row that's why I didn't do a loop for it. – villager Jan 21 '13 at 03:44
  • The only other thing I can think of is that it is getting a corrupt parcel back. – iagreen Jan 21 '13 at 03:49
  • Can you elaborate on the "corrupt parcel". This exception is not always encountered, but there are times where it is encountered, I wonder if it is because of other things. – villager Jan 21 '13 at 04:05
  • @mai. The code is failing in `CursorWindow$1.createFromParcel`, which is attempting to create a `CursorWindow` from a `Parcel` object. If it is not failing on the allocation, it could be failing because it is not getting data for the parcel correctly. I can only speculate on why that might be -- perhaps the connection to the `contentResolver` is not longer valid (like keeping a stale handle to it during device rotation, background thread, etc), the other end of the content resolver is not there some reason (no data connection, SD card mounted, etc.), or some other issue – iagreen Jan 21 '13 at 04:29
  • @mai if you want single result then use order by i.e. last parameter of function and include limit for example use "id DESC limit 1" for order your result according to id with single result – Jaiprakash Soni Jan 21 '13 at 06:16

3 Answers3

28

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.

SMR
  • 6,320
  • 2
  • 32
  • 55
Justin Breitfeller
  • 13,290
  • 4
  • 36
  • 47
1

Try another thread

new Thread(new Runnable(){ public void run(){

...here all code

}});

. But by Android SDK source codes look like 4.0.2_r1

130  private CursorWindow(Parcel source) {
131 mStartPos = source.readInt();
132 mWindowPtr = nativeCreateFromParcel(source);
133 if (mWindowPtr == 0) {
134 throw new CursorWindowAllocationException("Cursor window could not be "
135 + "created from binder.");
136 }
137 mName = nativeGetName(mWindowPtr);
138 mCloseGuard.open("close");
139 }
where mWIndowPtr is Int
Mertuarez
  • 861
  • 7
  • 24
-1

Try out this way:

 if (cursor != null) {
  cursor.moveToFirst();
   do {
   first = cursor.getString(cursor.getColumnIndex("first"));
  }while(cursor.moveToNext());

}

GrIsHu
  • 28,433
  • 10
  • 61
  • 99