【问题标题】:Android SQLite CursorWindowAllocationException crashAndroid SQLite CursorWindowAllocationException 崩溃
【发布时间】:2012-08-19 19:45:35
【问题描述】:

当我发出多个 cursor.moveToNext() 请求时,我的程序崩溃了。错误信息如下:

android.database.CursorWindowAllocationException: Cursor window allocation of 2048 kb failed. # Open Cursors=773 (# cursors opened by this proc=773)
at android.database.CursorWindow.<init>(CursorWindow.java:112)
at android.database.CursorWindow.<init>(CursorWindow.java:100)
at android.database.AbstractWindowedCursor.clearOrCreateWindow(AbstractWindowedCursor.java:198)
at android.database.sqlite.SQLiteCursor.clearOrCreateWindow(SQLiteCursor.java:364)
at android.database.sqlite.SQLiteCursor.fillWindow(SQLiteCursor.java:162)
at android.database.sqlite.SQLiteCursor.getCount(SQLiteCursor.java:156)
at android.database.AbstractCursor.moveToPosition(AbstractCursor.java:161)
at android.database.AbstractCursor.moveToNext(AbstractCursor.java:209)
at net.cunniman.teacherplannerlite.SchoolClassDataSource.getClassForDayView(SchoolClassDataSource.java:173)
at net.cunniman.teacherplannerlite.DayView.getSchoolClasses(DayView.java:200)
at net.cunniman.teacherplannerlite.DayView.displayDate(DayView.java:176)
at net.cunniman.teacherplannerlite.DayView.plusDay(DayView.java:287)
at net.cunniman.teacherplannerlite.DayView.onClick(DayView.java:93)
at android.view.View.performClick(View.java:3565)
at android.view.View$PerformClick.run(View.java:14165)
at android.os.Handler.handleCallback(Handler.java:605)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4514)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:980)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:747)
at dalvik.system.NativeStart.main(Native Method)

try {
    this.openForRead();
    for (int day_counter = 0; day_counter < DAY_PERIOD.length/2; day_counter++)
    {
        Cursor cursor = database.rawQuery
            ("SELECT " + NAME + " FROM " + TABLE_NAME + " WHERE " + DAY_PERIOD[day_counter * 2]
            + " = '" + day + "' AND " + DAY_PERIOD[day_counter * 2 + 1] + " = "
            + Integer.toString(period), null);


    while (cursor.moveToNext())
    {
        String name = cursor.getString(0);
        sc.setName(name);
    }


} finally {
    this.close();
}

有谁知道可能导致这种情况的原因 - 似乎是内存泄漏(也许?),但有什么办法可以防止它发生吗?

谢谢

【问题讨论】:

  • 你可能没有关闭所有的光标
  • 您没有关闭所有游标。在while循环之后,添加cursor.close。您还创建了很多 Cursor 对象。我建议检查您的代码。最好做一个“更大”的 SQL,获取所有数据,然后循环。
  • 感谢汤姆,这对双方都非常有帮助,而且很有意义。我明天会尝试实施。
  • 我在模拟器上遇到了类似的问题,但在我测试过的任何设备上都没有。但是,我正在关闭所有游标。有什么想法吗?

标签: android android-cursor


【解决方案1】:

大多数情况下,导致此错误的原因是未关闭的游标。确保在使用后关闭所有游标(即使出现错误)。

Cursor cursor = null;
try {
    cursor = db.query(...
    // do some work with the cursor here. i.e.:
    while (cursor.moveToNext())
    { .... }
} finally {
    // this gets called even if there is an exception somewhere above
    if(cursor != null)
        cursor.close();
}

【讨论】:

  • 我在模拟器上遇到了类似的问题,但在我测试过的任何设备上都没有。但是,我正在关闭所有游标。有什么想法吗?
  • 抱歉,如果没有任何具体细节,我无法提供帮助。最好为此提出一个新问题。
  • 是的,有感觉。就我而言,这是一个相当复杂的问题,所以我将先重写所有代码,看看是否有帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-15
  • 2013-04-12
  • 2013-05-18
相关资源
最近更新 更多