0

I've got some seriuos problem! My application is in Google Play, today I visited developer console @ Google Play and I see two error logs - both them are about same thing.

Here is the stacktrace:

    java.lang.IllegalStateException: attempt to re-open an already-closed object: SQLiteDatabase: /data/data/pl.snowvision/databases/snow_pilot
    at android.database.sqlite.SQLiteClosable.acquireReference(SQLiteClosable.java:55)
    at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1156)
    at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1032)
    at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1200)
    at pl.snowvision.helpers.Entity.getEntities(Entity.java:139)
    at pl.snowvision.fragments.stations.StationsList.showListByName(StationsList.java:163)
    at pl.snowvision.fragments.stations.StationsList.onCreateView(StationsList.java:140)
    at android.support.v4.app.Fragment.performCreateView(Fragment.java:1460)
    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:911)
    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
    at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:429)
    at android.os.Handler.handleCallback(Handler.java:615)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:4898)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
    at dalvik.system.NativeStart.main(Native Method)

and below I include method called in StationsList.java:163 which causes the problem:

    public static ArrayList<Entity> getEntities(String entityId, String catId, String parentId, Context context, String... orderKeys) {
            ArrayList<Entity> entities = new ArrayList<Entity>();
            DbHelper database = DbHelper.getInstance(context);
            SQLiteDatabase db = database.getReadableDatabase();

            String whereClause = "";
            if(entityId != null)
                whereClause = EntityTable.KEY_ID + " = " + "\"" + entityId + "\"";
            if(catId != null) {
                if(entityId != null)
                    whereClause += " AND ";
                whereClause += EntityTable.KEY_CATEGORYID + " = " + "\"" + catId + "\"";
            }
            if(parentId != null) {
                    if(catId != null || (catId == null && entityId != null))
                        whereClause += " AND ";                 
                whereClause += EntityTable.KEY_PARENTID + " = " + "\"" + parentId + "\"";   
            }
            Cursor eCursor = db.query(EntityTable.TABLE_NAME, null, whereClause, null, null, null, null);
            if(eCursor != null && eCursor.getCount() > 0) {
                while(eCursor.moveToNext()) {
                    String id = eCursor.getString(eCursor.getColumnIndex(EntityTable.KEY_ID));
                    String categoryId = eCursor.getString(eCursor.getColumnIndex(EntityTable.KEY_CATEGORYID));
                    String pId = eCursor.getString(eCursor.getColumnIndex(EntityTable.KEY_PARENTID));
                    Entity tmpEntity;
                    if(orderKeys != null && orderKeys.length > 0)
                        tmpEntity = new Entity(id, categoryId, pId, new String[]{orderKeys[0]});
                    else
                        tmpEntity = new Entity(id, categoryId, pId);
                    Cursor iCursor = db.query(ItemsTable.TABLE_NAME, null, ItemsTable.KEY_PARENTID + "=\"" + id + "\"", null, null, null, null);
                    if(iCursor != null) {
                        while(iCursor.moveToNext()) {
                            String key = iCursor.getString(iCursor.getColumnIndex(ItemsTable.KEY_KEY));
                            String value = iCursor.getString(iCursor.getColumnIndex(ItemsTable.KEY_VALUE));
                            tmpEntity.addPair(key, value);
                        }
                    }
                    iCursor.close();
                    entities.add(tmpEntity);
                }
            }
            eCursor.close();
            return entities;
        } 

Do you have any ideas what exactly may cause this problem & how to fix it ? Thanks for any help!

botang
  • 89
  • 2
  • 7
  • probably you have closed your database object some where, without opening, accessing it is giving this error.. – techieWings Jan 28 '13 at 08:17
  • http://stackoverflow.com/questions/2493331/what-are-the-best-practices-for-sqlite-on-android – android2013 Jan 28 '13 at 08:23
  • I know that database is closed at time of this function execution, but I have no idea why it is closed - this code doesn't close it, nowhere else i do no close database. Did someone else get the same error anytime ? – botang Jan 28 '13 at 08:26
  • @Albert: your database instance is local ,make it global if you want to use it elsewhere ..and one more thing if you want to add comment to specific answer then select "add comment" option below that answer and put your comment over there so that the person who has answer your question get notify that someone comment on his answer. – dd619 Jan 28 '13 at 10:54

1 Answers1

1

Actually by using SQLiteDatabase db = database.getReadableDatabase(); you are opening an database instance.And at the end you are not closing this instance.hence next time it gives you the above error.

To resolve this issue, close your database before exiting from the function.

In your code put the below statement beforereturn entities;

db.close();

And if you are closing database elsewhere then you have to open it again before for further use.

dd619
  • 5,910
  • 8
  • 35
  • 60