2012-04-07 13 views
0

ich mit einem Cursor einen Fehler haben, wenn ich eine Datenbank gerade lese:Fehler mit Cursor und SQLite in 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) 

Es ist auf der Linie

cursor.getCount() 

Die gesamte Datei 8kb ist passiert, es hat 32 Zeilen, aber IDs gehen von 737 bis 768, ist das Problem von dort?

(Ich habe bemerkt, dass, wenn ID unter 600 ist, gibt es keine Probleme)

+0

Mehr Code wäre hilfreich ... Die Fehlermeldung scheint darauf hinzuweisen, dass Sie eine Menge Cursor gleichzeitig öffnen. – Barak

Antwort

0

(Frage der OP in Frage bearbeiten beantwortet. Umgerechnet auf ein Community Wiki Antwort. Siehe Question with no answers, but issue solved in the comments (or extended in chat))

Die OP schrieb:

Ich löste das Problem. Mein Code war:

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

ich geändert:

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

Ich dachte, dass der Cursor auf onDestroy geschlossen wurde().

0

Sie sollten den Cursor in einem finally Block schließen, um sicherzustellen, dass es geschlossen ist, auch wenn eine Ausnahme auftritt.

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