【发布时间】:2018-07-02 07:58:52
【问题描述】:
我正在使用 sqlite DB 来存储我的用户在线时的位置,但由于某种原因,我收到了这个崩溃报告,我不知道它是什么或如何修复它。
android.database.CursorWindowAllocationException: Cursor window allocation of 2048 kb failed.
at android.database.CursorWindow.<init>(CursorWindow.java:108)
at android.database.AbstractWindowedCursor.clearOrCreateWindow(AbstractWindowedCursor.java:198)
at android.database.sqlite.SQLiteCursor.fillWindow(SQLiteCursor.java:138)
at android.database.sqlite.SQLiteCursor.getCount(SQLiteCursor.java:132)
at com.tech.databases.Routes_DB.savedGPSHasEntries(Routes_DB.java:109)
at com.tech.activity.Menu_dashboard.onLocationChanged(Menu_dashboard.java:2602)
at com.google.android.gms.c.b.r.a(Unknown:-1)
at com.google.android.gms.common.api.internal.h.b(Unknown:-1)
at com.google.android.gms.common.api.internal.h$c.handleMessage(Unknown:-1)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6314)
at java.lang.reflect.Method.invoke(Method.java:-2)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:872)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762)
我查到的每件事都说这是由于没有关闭我的光标造成的,但是在我的代码中我每次都关闭它:
public boolean savedGPSHasEntries(){
boolean hasEntry = false;
String countQuery = "SELECT * FROM " + TABLE_SAVED_ROUTE;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(countQuery, null);
int cnt = cursor.getCount();
if (cnt > 0)
hasEntry = true;
// Log.e("Database", "Count: " + cnt);
cursor.close();
return hasEntry;
}
这就是我查询数据的方式:
if (routesDB.savedGPSHasEntries()) {
Log.e(TAG, "DB isn't empty");
}
所以我对这个错误感到困惑,甚至不知道如何修复它。
【问题讨论】:
-
你也不要关闭数据库。
-
不要使用
"SELECT * FROM而是使用count()函数。此外,每当您从数据库中获取数据时,尽量不要使用SELECT *,而是提供您需要的所有列名。 -
cursor.close(); db.close(); -
有一个类
DatabaseUtils为此提供了实用方法。见stackoverflow.com/a/18098603/7995966。 -
从错误日志 android.database.CursorWindowAllocationException: 2048 kb 的光标窗口分配失败,您的光标似乎超出了 2MB 的数据限制,您是否正在保存较大的二进制数据(图片)?这可能是导致问题的原因
标签: android sqlite database-cursor