I get this error after going to this activity, starting a new one, coming back. It doesn’t happen when I first load the activity. functionally everything works… but I still get this error.
ERROR/Cursor(1059): Finalizing a
Cursor that has not been deactivated
or closed. database =
/data/data/com.roger.testapp/databases/data,
table = null, query = SELECT MAX(_id)
FROM record03-02 16:47:21.835:
ERROR/Cursor(1059):
android.database.sqlite.DatabaseObjectNotClosedException:
Application did not close the cursor
or database object that was opened
here
In onCreate:
mDbHelper = new CommonDbAdapter(this);
mDbHelper.open();
fillData();
fillData() calls fetchNote in my dbhelper, the cursor is used for a couple things, if rowId == 0, that means I didn’t select an item to get into this activity and I want to get the last row in that table. if rowId = something else, then I grab that row. I think the problem is in here somewhere, I’m just not sure.
public Cursor fetchNote(long rowId, String table, String columns) throws SQLException {
if (rowId == 0) {
String query = "SELECT MAX(_id) FROM record";
Cursor cursor = mDb.rawQuery(query, null);
rowId = 0;
if (cursor.moveToFirst())
{
rowId = cursor.getInt(0);
}
}
Cursor mCursor =
mDb.query(true, table, new String[] {KEY_ROWID,
columns}, KEY_ROWID + "=" + rowId, null,
null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
In onDestroy:
super.onDestroy();
if (mDbHelper != null) {
mDbHelper.close();
}
Also, I am startManagingCursor
Don’t close your database in
onDestroy. Close it immediately after you’re done using it(make sure every cursor is closed before the database is closed). onDestroy may not be called when you expect.Also, close your
Cursorobject after you’re done using it.Edits:
Since your activity is managing your
Cursor, you may consider stop managing it and closing everything in theonPausemethod, and inonResumeopen everything up andfillDataonce again. If you could change your code around so you dont rely on your activity managing the cursor, you wouldn’t need to hold into open database objects and worry about them.