DB wird nicht geschlossen

  • 2 Antworten
  • Neuester Beitrag
Diskutiere DB wird nicht geschlossen im Android App Entwicklung im Bereich Betriebssysteme & Apps.
S

spike

Neues Mitglied
Hi Leute,

ich habe ein Problem, das ich bisher leider noch nicht lösen konnte.
Ich öffne in meiner Activit eine Datenbank-Verbindung.
Das mache mit Hilfe eines DB Helfers

Code:
[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055][LEFT]private[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]void[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] openDataBase(){
[/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]myDataBase[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] = [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]null[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]; 
[/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]mCursor[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] = [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]null[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];
[/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]mHelper[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] = [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]new[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] DatenbankManager([/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]this[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]);
[/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]try[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] { 
[/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]mHelper[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].createDataBase(); 
} [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]catch[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] (IOException ioe) { 
[/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]throw[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]new[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] Error([/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"Unable to create database"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]);
}

[/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]try[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] { 
[/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]mHelper[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].openDataBase();
[/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]myDataBase[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] = [/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]mHelper[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].getDatabase();
} [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]catch[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2](SQLException sqle){ 
[/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]throw[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] sqle; 
}[/LEFT]
}
[/SIZE]
In meiner onPause und onDestroy Methode schließe ich die DB wieder:
mHelper.close();

Wenn ich nun in meiner Activity eine Kontextmenü aufrufe und dort irgendwas auswähle, bekomme ich immer diesen Fehler:

close() was never explicitly called on database '/mnt/sdcard/de.androiddev.mytestapp/test.db'

Ich schließe doch aber die DB oder etwa nicht?
Mittels Google Suche konnte ich leider keine Lösung dafür finden. Dort steht überall das man in onPause und onDestroy die DB schließen soll und das mach ich ja....


Gruß

Spike
 
T

Tom299

Stammgast
du benutzt doch sicher irgendwo einen cursor, um daten zu lesen, oder? wenn du den cursor nicht schließt, bekommst du auch diese oder ne ähnliche fehlermeldung, habs nicht mehr genau im kopf. vielleicht liegts ja daran ;-)
 
S

spike

Neues Mitglied
Hi, ja ich verwende einen Cursor in meiner onStart-Methode:
mCursor​
= myDataBase.rawQuery(RAW, null);

startManagingCursor(
mCursor);

Meine Close Methode habe ich nun wie folgt:

private
void closeDataBase(){
if (mHelper != null){
mHelper.close();
}
if (mCursor != null){
mCursor.close();
}

}

Diese Methode rufe ich in der onPause und onDestroy-Methode auf. An der Fehlermeldung hat sich aber leider nichts geändert...

Gruß

Spike