0

Also it never happened on any of my devices, I am receiving quite a lot crash reports from customers like:

java.lang.IllegalStateException: attempt to re-open an already-closed object

Crashes always happens on db.query line (7th line of code):

        ArrayList<AlarmItem> items = new ArrayList<AlarmItem>();
        SQLiteDatabase db = null;
        Cursor cursor = null;

        try {
            db = instance.getReadableDatabase();
            // CRASH next line:       
            cursor = db.query(TABLE_ALARMS, null, null, null, null, null, null); 
            ...
            }

        } catch (SQLiteException ex) {
            ...
        } finally {
            if (cursor != null)
                cursor.close();

            if (db != null)
                db.close();
        }

        return items;

Anybody know what can be the reason? Thanks!

EDIT:

All methods are synchronized, so there can't be more than 1 operation in a moment. Instance is a singleton, so it shouldn't be problem.

qkx
  • 2,383
  • 5
  • 28
  • 50
  • Are you protecting against things like calls to finish() that might be occurring in parallel? Also is 'instance' initialized in onCreate or at the class level? It sounds like a race condition related to the unexpected threading from your development environment. E.g., a multi-core device. – caskey Aug 13 '14 at 07:52
  • change instance.getReadableDatabase() to instance.getWritableDatabase(); – Naveed Ali Aug 13 '14 at 07:54
  • @caskey all methods are synchronized, so there can't be more than 1 operation in a moment. Instance is singleton, so it shouldn't be a problem.. – qkx Aug 13 '14 at 07:56
  • @NaveedAli He shouldn't need a writable db to query. – Gabe Sechan Aug 13 '14 at 07:57
  • @Naveed - why? I just need to read database, not write to it – qkx Aug 13 '14 at 07:57
  • @qkx Synchronization doesn't protect against everything. It prevents two methods from being run at once, but it doesn't prevent a method from being run after the database helper has been closed or after the db has been closed if the database object is cached anywhere. – Gabe Sechan Aug 13 '14 at 08:00
  • see http://developer.android.com/reference/android/database/sqlite/SQLiteOpenHelper.html – Naveed Ali Aug 13 '14 at 08:01

3 Answers3

0

This can be happen if you are attempting to assign result to Cursor, which was already closed. Have you tried to determine wheter it was closed?

if  (!cursor.isClosed()){
   // do stuff here
}
Stepan Tuhacek
  • 219
  • 5
  • 18
  • question updated with full method code, it should be impossible for cursor object to be already closed...check code – qkx Aug 13 '14 at 08:04
0

I suggest to try this:

if (db == null || !db.isOpen())
    db = getReadableDatabase();

to make sure you dont open it twice

nouseforname
  • 720
  • 1
  • 5
  • 17
  • question updated with full method code, it should be impossible for db object to be already opened, check code – qkx Aug 13 '14 at 08:03
0

You should select something like

Cursor c = sqLiteDatabase.query("table1", tableColumns, whereClause, whereArgs,
null, null, orderBy); 

Also, see this question https://stackoverflow.com/a/10601764

Community
  • 1
  • 1
QArea
  • 4,955
  • 1
  • 12
  • 22