0

读取数据库时,游标出错:

04-07 18:11:25.672: ERROR/AndroidRuntime(5801): android.database.CursorWindowAllocationException: Cursor window allocation of 2048 kb failed. # Open Cursors=666 (# cursors opened by this proc=666)

它发生在线上

cursor.getCount()

整个文件是 8kb,它有 32 行,但 ID 从 737 到 768,问题出在哪里?

(我注意到如果ID低于600,没有问题)

4

2 回答 2

0

You should really be closing the cursor in a finally block, to ensure that it's closed even if an exception happens.

public News getNewsWithID(int id){
    Cursor c = bdd.query(TABLE_NEWS, new String[] {COL_ID, COL_ASSO, COL_DATE, COL_HEURE, COL_TYPE, COL_TITRE, COL_CONTENU, COL_SERVEURID}, COL_ID +"='"+ id +"'" , null, null, null, null);
    try {
        return cursorToNews(c);
    } finally {
        c.close();
    }
}
于 2015-01-30T12:02:49.350 回答
0

(OP 在问题编辑中回答的问题。转换为社区 wiki 答案。请参阅没有答案的问题,但问题在评论中解决(或在聊天中扩展)

OP写道:

我解决了这个问题。我的代码是:

public News getNewsWithID(int id){
        Cursor c = bdd.query(TABLE_NEWS, new String[] {COL_ID, COL_ASSO, COL_DATE, COL_HEURE, COL_TYPE, COL_TITRE, COL_CONTENU, COL_SERVEURID}, COL_ID +"='"+ id +"'" , null, null, null, null);
        return cursorToNews(c);
    }

我改为:

public News getNewsWithID(int id){
        Cursor c = bdd.query(TABLE_NEWS, new String[] {COL_ID, COL_ASSO, COL_DATE, COL_HEURE, COL_TYPE, COL_TITRE, COL_CONTENU, COL_SERVEURID}, COL_ID +"='"+ id +"'" , null, null, null, null);
        News temp = cursorToNews(c);
        c.close();
        return temp;
    }

我认为光标在 onDestroy() 上已关闭。

于 2015-01-30T11:59:00.467 回答