SQLite数据库游标崩溃

时间:2018-07-02 07:58:52

标签: android sqlite database-cursor

我正在使用sqlite数据库存储用户在线时的位置,但是由于某些原因,我收到了崩溃报告,但不知道它是什么或如何解决。

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");
}

所以我对此错误或什至如何开始找出解决方法感到困惑。

1 个答案:

答案 0 :(得分:2)

CursorWindowAllocationException

  

当无法分配CursorWindow时,抛出此异常,   很有可能是由于内存不可用。

您应该使用

  • String countQuery = "SELECT count(*) FROM " + TABLE_SAVED_ROUTE;
  • 请确保还关闭您的数据库

演示

cursor.close(); 
db.close();

尝试

String countQuery = "SELECT count(*) FROM " + TABLE_SAVED_ROUTE;

    Cursor cursor = db.rawQuery(countQuery, null);
    cursor.moveToFirst();
    int count = cursor.getInt(0);
    if(count>0)
    {

    }

有关更多信息,请参见How to get row count in sqlite using Android?