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)
...
为什么会抛出这个异常的任何想法?谢谢!
答案 0 :(得分:25)
我怀疑错误可能与您没有正确关闭游标有关。尝试:
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
}
应始终关闭光标(无论其是否为空)。确保您的应用的其余部分也是这样做的。
答案 1 :(得分:1)
尝试另一个帖子
new Thread(new Runnable(){ public void run(){
...here all code
}});
。但是Android SDK源代码看起来像4.0.2_r1
130 private CursorWindow(Parcel source) {其中 mWIndowPtr 是 Int
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 }
答案 2 :(得分:0)
尝试这种方式:
if (cursor != null) { cursor.moveToFirst(); do { first = cursor.getString(cursor.getColumnIndex("first")); }while(cursor.moveToNext());
}