2012-04-07 36 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(); 
    } 
}