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 }

然后在第 3 行(根据日志),我不时遇到这个异常(摘录如下):

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)

...

任何想法为什么它会抛出这个异常?谢谢!

4

3 回答 3

28

我怀疑该错误可能与您没有始终正确关闭游标有关。尝试:

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
}

游标应始终关闭(无论其是否为空)。确保您的应用程序的其余部分也这样做。

于 2013-01-23T16:07:31.713 回答
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

于 2013-01-23T22:04:01.153 回答
-1

试试这种方式:

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

}

于 2013-01-23T15:55:45.190 回答