0

I am using a sqlite DB to store my users location while they are online, but for some reason I am getting this crash report and I don't know what it is or how to fix it.

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)

Every thing I have looked up says that it is caused by not closing my cursor, but on my code I close it every single time:

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

And this is how I query the data:

if (routesDB.savedGPSHasEntries()) {
    Log.e(TAG, "DB isn't empty");
}

So I am confused about this error or how to even begin to find out how to fix it.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
user2101081
  • 445
  • 5
  • 22
  • 1
    You don't also close db. –  Jul 02 '18 at 08:05
  • 2
    DO not use `"SELECT * FROM` Use `count()` function instead . Also whenever you get data from database try not to use `SELECT *` instead of it provide all column names you needed . – ADM Jul 02 '18 at 08:07
  • 3
    `cursor.close(); db.close();` – IntelliJ Amiya Jul 02 '18 at 08:08
  • There is a class `DatabaseUtils` which provide utility methods for this . See https://stackoverflow.com/a/18098603/7995966. – ADM Jul 02 '18 at 08:11
  • From the error log android.database.CursorWindowAllocationException: Cursor window allocation of 2048 kb failed, it seems your cursor is exceeding the 2MB limit of data, Are you saving the large binary data (Image)? That might be causing the issue – Ankit Dubey Jul 02 '18 at 08:25
  • @AnkitDubey no, I am saving LatLng locations. – user2101081 Jul 02 '18 at 08:26

1 Answers1

2

CursorWindowAllocationException

This exception is thrown when a CursorWindow couldn't be allocated, most probably due to memory not being available.

You should use

  • String countQuery = "SELECT count(*) FROM " + TABLE_SAVED_ROUTE;
  • Make sure, Close your DB also .

DEMO

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

Try with

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

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

    }

For more info, See How to get row count in sqlite using Android?

IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198