0

我有一些严重的问题!我的应用程序在 Google Play 中,今天我访问了开发者控制台@Google Play,我看到了两个错误日志——它们都是一样的。

这是堆栈跟踪:

    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)

下面我包括在 StationsList.java:163 中调用的方法,这会导致问题:

    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;
        } 

你有什么想法可能导致这个问题以及如何解决它?谢谢你的帮助!

4

1 回答 1

1

实际上,通过使用SQLiteDatabase db = database.getReadableDatabase();你正在打开一个数据库实例。最后你没有关闭这个实例。因此下次它会给你上面的错误。

要解决此问题,请在退出函数之前关闭数据库。

在您的代码中将以下语句放在前面return entities;

db.close();

如果您在其他地方关闭数据库,那么您必须再次打开它才能进一步使用。

于 2013-01-28T08:25:29.883 回答