android-SQLCipher -Application没有关闭这里打开的游标或数据库对象

时间:2014-07-06 15:47:24

标签: android android-sqlite sqlcipher

我曾经有一个数据库,没有错误,我的数据库来自资产文件夹,它已经用SQLCipher编码

之后,在我的所有活动中,我收到错误,表示游标或数据库已打开且未关闭。

@Override
protected void onCreate() {
    super.onCreate();
    myDb = new DBAdapter(this);
    myDb.open();
    cursor = myDb.getFavorits();
}
@Override
protected void onDestroy() {
    super.onDestroy();
    myDb.close();
    cursor.close();
}
@Override
protected void onStop() {
    super.onStop();
        myDb.close();
        cursor.close();
}

这是打开和关闭的DBAdapter类:

public void open() {
    SQLiteDatabase.loadLibs(context);
    File databaseFile = context.getDatabasePath("mydb");
    db = SQLiteDatabase.openOrCreateDatabase(databaseFile,"mypass", null);

    }
public void close() {
    Log.v("this","dbclose");
    db.close(); 
}

public Cursor getFavorits() {
    Cursor c = db.query(DATABASE_TABLE, ALL_KEYS, null, null, null, null,null, null);
    if (c != null) {
        c.moveToFirst();
    }

    return c;
}

但是当我离开此活动时,它会向我显示此错误:

 Finalizing a Cursor that has not been deactivated or closed. database = /data/data/pachagename/databases/mydb, table = null, query = SELECT _id from favorits
07-06 15:42:42.769: E/Cursor(1214): net.sqlcipher.database.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here
你可以帮我找出并解决这个问题吗?

更新 我现在注意到一些新的东西,当我进入活动而没有离开活动时,它显示我这个错误!!我该怎么办 ?

2 个答案:

答案 0 :(得分:0)

onDestroy之后调用

onStop。我相信你正试图关闭已经关闭的东西。尝试在onDestroy方法中只留下super.onDestroy();

编辑:在黑暗中一枪:

当您将getFavorits()更改为private static method并从onCreate中调用它时会发生什么?cursor = DBAdapter.getFavorits()

答案 1 :(得分:0)

我相信您的问题是您在关闭 Cursor 之前关闭了数据库。当游标访问数据库时,应该在数据库之前关闭游标。