Android :CursorWindowAllocationException- 即使关闭游标

Android :CursorWindowAllocationException- even when closing the cursor

即使在关闭游标后,我仍然遇到上述异常。

下面是我的代码

public Cursor fetchAllFreeGradeNr(String grade) {
        open();
        String query;
            query = "SELECT DISTINCT " + C_GRADE + "," + C_GRADE_NR + ""
                    + " FROM '" + TABLE_NAME_PAID + "'" + " WHERE " + C_GRADE
                    + " = '" + grade + "'  ORDER BY " + C_GRADE_NR + " ASC";

            Cursor mCursor = mDb.rawQuery(query, null);
            if (mCursor != null) {
                mCursor.moveToNext();
            }
            close();
            return mCursor;
    }

崩溃发生在行 mCursor.moveToNext();

下面是我的日志

03-31 12:27:42.433: E/AndroidRuntime(25885): android.database.CursorWindowAllocationException: Cursor window allocation of 2048 kb failed. # Open Cursors=631 (# cursors opened by this proc=631)
03-31 12:27:42.433: E/AndroidRuntime(25885):    at android.database.CursorWindow.<init>(CursorWindow.java:108)
03-31 12:27:42.433: E/AndroidRuntime(25885):    at android.database.AbstractWindowedCursor.clearOrCreateWindow(AbstractWindowedCursor.java:198)
03-31 12:27:42.433: E/AndroidRuntime(25885):    at android.database.sqlite.SQLiteCursor.clearOrCreateWindow(SQLiteCursor.java:316)
03-31 12:27:42.433: E/AndroidRuntime(25885):    at android.database.sqlite.SQLiteCursor.fillWindow(SQLiteCursor.java:142)
03-31 12:27:42.433: E/AndroidRuntime(25885):    at android.database.sqlite.SQLiteCursor.getCount(SQLiteCursor.java:136)
03-31 12:27:42.433: E/AndroidRuntime(25885):    at android.database.AbstractCursor.moveToPosition(AbstractCursor.java:197)
03-31 12:27:42.433: E/AndroidRuntime(25885):    at android.database.AbstractCursor.moveToNext(AbstractCursor.java:245)
03-31 12:27:42.433: E/AndroidRuntime(25885):    at com.tss.in.database.ISOFitsProvider.fetchAllFreeGradeNr(ISProvider.java:151)

也试过如下

public Cursor fetchAllFreeGradeNr(String grade) {
        //Cursor mCursor ;
        open();
        String query;
            query = "SELECT DISTINCT " + C_GRADE + "," + C_GRADE_NR + ""
                    + " FROM '" + TABLE_NAME_PAID + "'" + " WHERE " + C_GRADE
                    + " = '" + grade + "'  ORDER BY " + C_GRADE_NR + " ASC";
        Cursor cursor = null;
        try {
            cursor = mDb.rawQuery(query, null);
            if (cursor != null) {
                return cursor;
            }
        } catch (Exception e) {
            if (cursor != null) {
                cursor.close();
            }
        }

        return cursor;
}

上面的代码也给crash了。

导致此错误的最常见原因是游标未关闭。确保在使用后关闭所有游标

if(mCursor!= null) mCursor.close();

你应该在完成后关闭游标,我的意思是当你从它获取数据并填充你的模型时你必须关闭它,所以你的方法可能是:

public Cursor fetchAllFreeGradeNr(String grade) {
        open();
        String query;
            query = "SELECT DISTINCT " + C_GRADE + "," + C_GRADE_NR + ""
                    + " FROM '" + TABLE_NAME_PAID + "'" + " WHERE " + C_GRADE
                    + " = '" + grade + "'  ORDER BY " + C_GRADE_NR + " ASC";

            Cursor mCursor = mDb.rawQuery(query, null);
            return mCursor;
    }

然后在其他方法中,例如你有这样的东西:

   if (cursor != null && cursor.getCount() > 0) {
        cursor.moveToFirst();
        do {
            //... retrieve data
        } while (cursor.moveToNext());
    }

    if(cursor != null) cursor.close();