Problem description
This exception occurs if the app did not close the cursor or database before it opens the database.
Solution
This exception occurs because the app did not close the cursor or database. You can close the cursor and the database by invoking the close method on the previously opened database.
Sample code
android.database.sqlite.DatabaseObjectNotClosedException:Application did not close the cursor or database object that was opened here
android.database.sqlite.DatabaseObjectNotClosedException:Application did not close the cursor or database object that was opened here
at android.content.ContentResolver.query(ContentResolver.java:399)
at android.content.ContentResolver.query(ContentResolver.java:316)Procedure
1. Execute the try-catch statement to catch the exception and close the database in the finally section.
OpenHelper dh = new DatabaseHelper(this);
SQLiteDatabase db = null;
try {
db = dh.getWritableDatabase();
} catch(SQLiteException e){
e.printStackTrace();
} finally {
if(db != null && db.isOpen())
db.close();
}2. During the Activity lifecycle, invoke the onResume() method to open the database and the onPause() method to close the database.
OpenHelper dh = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
dh = new DatabaseHelper(this);
}
@Override
protected void onResume() {
this.db = dh.getWritableDatabase();
}
@Override
protected void onPause( {
if(db != null && db.isOpen()){
db.close();
}
}