2012-04-07 55 views
0

光标和SQLite我有,当我读数据库中的光标的错误:错误与Android的

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,不存在任何问题)

+0

更多的代码会有帮助...错误消息似乎表明您正在打开大量的游标。 – Barak 2012-04-10 04:09:46

回答

0

(问题由一个问题编辑OP回答。转换为社区维基答案。见Question with no answers, but issue solved in the comments (or extended in chat)

的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()。

0

您应该真的关闭finally区块中的光标,以确保即使发生异常也会关闭光标。

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