Android:CursorWindowAllocationException-即使关闭游标

时间:2016-03-31 07:11:16

标签: android exception android-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;
}

以上代码也会导致崩溃。

2 个答案:

答案 0 :(得分:1)

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

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

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

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

function randomColor() {
    var hexValues = '0123456789ABCDEF'.split('');
    var startColor = '#';
    for (var i = 0; i < 6; i++ ) {
        startColor += hexValues[Math.round(Math.random() * 15)];
    }
    return startColor;
}

var hexDigits = new Array("0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f");

function hex(x) {
  return isNaN(x) ? "00" : hexDigits[(x - x % 16) / 16] + hexDigits[x % 16];
}

function rgb2hex(rgb) {
 rgb = rgb.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/);
 return "#" + hex(rgb[1]) + hex(rgb[2]) + hex(rgb[3]);
}

$(function() {
    $(".inverseDiv").each(function() {
        $(this).css("background-color", randomColor());
    });
});

$(document).ready(function(){
  $('#myButton').click(function () {
        $(".inverseDiv").each(function() {
        var color = rgb2hex($(this).css("background-color"));

                color = color.substring(1); // remove #

                color = parseInt(color, 16); // convert to integer

                color = 0xFFFFFF ^ color; // invert three bytes

                color = color.toString(16); // convert to hex

                color = ("000000" + color).slice(-6); // pad with leading zeros

                color = "#" + color; // prepend #

                $(this).css("color", color);
        });
    }); 
}); 

答案 1 :(得分:0)

此错误的原因通常是非关闭游标。确保在使用后关闭所有游标

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