Piotr Piotr - 3 months ago 39x
Android Question

IllegalStateException: attempt to re-open an already-closed object: SQLiteDatabase using Loader

Log states:

W/Filter(1629): An exception occured during performFiltering()!
W/Filter(1629): java.lang.IllegalStateException: attempt to re-open an already-closed object: SQLiteDatabase: /data/data/com.example.bazadanych/databases/MyDb
W/Filter(1629): at android.database.sqlite.SQLiteClosable.acquireReference(SQLiteClosable.java:55)
W/Filter(1629): at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1156)
W/Filter(1629): at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1032)
W/Filter(1629): at com.example.bazadanych.DBAdapter.fetchCountriesByName(DBAdapter.java:186)
W/Filter(1629): at com.example.bazadanych.MainActivity$2.runQuery(MainActivity.java:74)
W/Filter(1629): at android.support.v4.widget.CursorAdapter.runQueryOnBackgroundThread(CursorAdapter.java:397)
W/Filter(1629): at android.support.v4.widget.CursorFilter.performFiltering(CursorFilter.java:50)
W/Filter(1629): at android.widget.Filter$RequestHandler.handleMessage(Filter.java:234)
W/Filter(1629): at android.os.Handler.dispatchMessage(Handler.java:102)
W/Filter(1629): at android.os.Looper.loop(Looper.java:136)
W/Filter(1629): at android.os.HandlerThread.run(HandlerThread.java:61)

As it is in the topic the filter doesn't filter when I go back to listview saying that the cursor has been closed.

The only issue left after using loader, the filter doesn't filter after moving to details activity and coming back to
. Any ideas?

This is the the filter:

public static Cursor fetchCountriesByName(String inputText) throws SQLException {
Log.w(TAG, inputText);
Cursor c = null;

if (inputText == null || inputText.length () == 0) {
c = db.query(DATABASE_TABLE, new String[] {KEY_ROWID,
null, null, null, null, null);
} else {
c = db.query(true, DATABASE_TABLE, new String[] {KEY_ROWID,
KEY_NAME + " like '%" + inputText + "%'", null,
null, null, null, null);

if (c != null) {
return c;


Varun was close and gave me a lead that I've followed.

Using CursorLoader helped with problems populating the database in a listview, now it loads with no problem, thanks to @Selvin.

The problem was obviously closing cursor, but as I mentioned it was happening only after moving to another activity where I was also using database and there the db has been closed, what affected the possibility of getting data again in the listview after backing to the listview, which used a cursor, but after closing it, retieving it was impossible.

To the problem was: db.close(); not in a listview acitvity but in the other activity. Thanks for all your help.