重新创建 SQLiteDatabase 错误 Activity

SQLiteDatabase error on recreate Activity

重新创建意图后,我在每个数据库查询中都遇到异常。我想这可能是因为重新创建的方式activity,但不知道如何解决这个问题。

我正在更改语言和暗模式更改后重新创建它。

重新创建时抛出异常 activity:

Attempt to re-open an already-closed object: SQLiteDatabase:

Activity

private lateinit var db: SQLiteDatabase
private lateinit var oh: LOTDatabaseHelper
...

onCreate

...
oh = LOTDatabaseHelper(this)
db = oh.readableDatabase
...

onDestroy

...
db.close()
oh.close()
...

以及我如何重新创建意图

val intent = intent
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION)
startActivity(intent)
finish()

编辑:

我发现 Activity 被创建了大约两次,然后留下一个,其余的被销毁。 OnDestroy 似乎适用于所有活动并关闭数据库,这应该是不可能的。

现在我导航到初始屏幕,然后导航到使用数据库的 Activity,一切正常。 那么,当 Activity 被替换为 herself 时,为什么生命周期无法正常工作?

打开一个已经关闭的数据库通常是由不匹配的打开和关闭引起的。

通常解决方法是不打开和关闭数据库(除非特别需要,例如从备份副本恢复数据库,或强制 WAL 检查点备份数据库),而是只打开数据库一次。

通常不需要一直打开和关闭数据库,因为当应用程序完成时,数据库将关闭(您可以随时关闭它最顶层 activity 的 onDestroy)。此外打开数据库是一种资源消耗,所以打开很多是资源 wasteful/inefficient.

  • 也许会看到 Android SQLite DB When to Close

我建议删除所有关闭。您可能还希望考虑单例方法,甚至可能在 Helper 中拥有所有数据库功能。

作为一个例子,也许可以考虑:-

帮手DBHelper :-

class LOTDatabaseHelper(context: Context): SQLiteOpenHelper(
    context,
    DATABASE_NAME,
    null,
    DATABASE_VERSION) {

    companion object {
        const val DATABASE_NAME = "my.db"
        const val DATABASE_VERSION = 1

        @Volatile
        private var instance: SQLiteDatabase? = null

        fun getInstance(context: Context): SQLiteDatabase {
            if (instance == null) {
                instance = LOTDatabaseHelper(context).writableDatabase
            }
            return instance as SQLiteDatabase
        }

    }

    override fun onCreate(db: SQLiteDatabase?) {
        db?.execSQL(Table1.CREATE_SQL)
    }

    override fun onUpgrade(db: SQLiteDatabase?, oldVersion: Int, newVersion: Int) {
        TODO("Not yet implemented")
    }

    override fun onDowngrade(db: SQLiteDatabase?, oldVersion: Int, newVersion: Int) {
        super.onDowngrade(db, oldVersion, newVersion)
    }

    fun insert(name: String): Long {
        val cv = ContentValues()
        cv.put(Table1.COL_NAME,name)
        return instance!!.insert(Table1.TABLE_NAME,null,cv)
    }

    fun getAllFromTable1() : Cursor {
        return instance!!.query(Table1.TABLE_NAME,null,null,null,null,null,"${Table1.COL_NAME} ASC")
    }

    @SuppressLint("Range")
    fun logAll() {
        val csr = getAllFromTable1()
        var row = 1
        val nameix = csr.getColumnIndex(COL_NAME)
        while (csr.moveToNext()) {
            Log.d("CURSORINFO","Row ${row++} ${COL_NAME} is ${csr.getString(csr.getColumnIndex(
                COL_NAME))}")
            //val test = csr.getString(csr.getColumnIndex("${COL_NAME}"))

        }
        csr.close()
    }

    // Table stuff
    class Table1 {
        companion object {
            const val TABLE_NAME = "table1"
            const val COL_ID = BaseColumns._ID
            const val COL_NAME = "${TABLE_NAME}_name"
            const val CREATE_SQL = "CREATE TABLE IF NOT EXISTS $TABLE_NAME (${COL_ID} INTEGER PRIMARY KEY, $COL_NAME TEXT);"
        }
    }
}

用于调用第二个 Activity MainActivity 的初始 Activity :-

class MainActivity : AppCompatActivity() {
    val TAG = "MAINACTIVITYINFO"

    private lateinit var dbother: DBHelper
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        dbother = DBHelper.getInstance(this)!!
        dbother.insert("TESTOTHER") // Use the Insert in the DBHelper

        /* of course you can still get an SQLiteDatabase */
        /* There is very little use getting readable as it gets writable */
        /* so get writable is more accurate description */
        var sqliteDatabase = dbother.writableDatabase //<<<<<< can be used
        logCursorInfo(dbother.allAsCursor)

        // NOW Start the other Activity
        Log.d(TAG,"starting Activity2")
        intent = Intent(this,Activity2::class.java)
        startActivity(intent)
        Log.d(TAG,"after starting Activity2")

    }

    override fun onDestroy() {
        super.onDestroy()
        Log.d("ONDESTROY","On Destroy invoked.")
    }

    override fun onResume() {
        super.onResume()
        Log.d(TAG,"On Resume invoked")
        dbother = DBHelper.getInstance(this)!!
    }

    //
    @SuppressLint("Range")
    fun logCursorInfo(csr: Cursor) {
        val nameix = csr.getColumnIndex(LOTDatabaseHelper.Table1.COL_NAME)
        val idix = csr.getColumnIndex(LOTDatabaseHelper.Table1.COL_ID)
        while (csr.moveToNext()) {
            //Log.d(TAG,"${LOTDatabaseHelper.Table1.COL_ID} = ${csr.getString(idix)} ${LOTDatabaseHelper.Table1.COL_NAME} = ${csr.getString(nameix)}")
            Log.d(TAG,"${csr.getString(csr.getColumnIndex(LOTDatabaseHelper.Table1.COL_NAME))}")
        }
    }
    companion object {
        var counter: Int = 0
    }
}
  • 这使用了 DBHelper 但获取了一个实例,然后它添加了几行,提取它们然后开始第二个 Activity。

Activity2 您遇到问题的 activity :-

class Activity2 : AppCompatActivity() {
    val TAG = "ACTIVITY2INFO"
    private lateinit var dbother: DBHelper
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_2)
        Log.d(TAG,"Activity 2 Started (onCreate). $this")

        dbother = DBHelper.getInstance(this)!! // Get the Database

        dbother.insert("TESTOTHER_ACTIVITY2") // Add a row
        var csr = dbother.allAsCursor // get All the rows
        DatabaseUtils.dumpCursor(csr) // dump the cursor
        csr.close() // close the cursor

        /* Start another activity (will loop so stop with counter)*/
        Log.d(TAG,"Restarting Activity. counter is ${counter++} $this")
        if (counter < 3) {
            val intent = intent
            intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION)
            startActivity(intent) //<<<<<<<<<< RUN 1 Not the Way as starts a new activity

            /* Perhaps the following???? */
            // this.intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION)
            //this.recreate() //<<<<<<<<<< RUN 2 perhaps the way but no intent
        }
        Log.d(TAG,"Finishing Activity 2 $this")
        finish()
    }

    companion object {
        var counter: Int = 0
    }

    override fun onResume() {
        super.onResume()
        Log.d(TAG,"OnResume Invoked. $this")
    }

    override fun onDestroy() {
        super.onDestroy()
        Log.d(TAG,"OnDestory (after super call - before db close and helper close. $this)")
        Log.d(TAG,"OnDestory (after all. $this)")
    }
}
  • 这会得到一个 DBHelper 实例,插入一行,提取所有数据,然后启动另一个 activity 注意这会无限循环,因此会出现计数器。

希望评论对您有所帮助。

如您所见,我包含了相当多的日志记录。但是,正如建议的那样,数据库永远不会关闭。

运行 应用程序

这是 运行 应用程序的结果,即日志(来自全新安装):-

2021-10-06 19:52:50.715  D/DBHELPERINFO: getInstance returning instance a.a.so69454766kotlinsqlite.DBHelper@59860ab
2021-10-06 19:52:50.717  D/MAINACTIVITYINFO: TESTOTHER
2021-10-06 19:52:50.717  D/MAINACTIVITYINFO: starting Activity2
2021-10-06 19:52:50.724  D/MAINACTIVITYINFO: after starting Activity2
2021-10-06 19:52:50.734  D/MAINACTIVITYINFO: On Resume invoked
2021-10-06 19:52:50.734  D/DBHELPERINFO: getInstance returning instance a.a.so69454766kotlinsqlite.DBHelper@59860ab
2021-10-06 19:52:50.848  D/ACTIVITY2INFO: Activity 2 Started (onCreate). a.a.so69454766kotlinsqlite.Activity2@3269902
2021-10-06 19:52:50.848  D/DBHELPERINFO: getInstance returning instance a.a.so69454766kotlinsqlite.DBHelper@59860ab
2021-10-06 19:52:50.850  I/System.out: >>>>> Dumping cursor android.database.sqlite.SQLiteCursor@950aa67
2021-10-06 19:52:50.851  I/System.out: 0 {
2021-10-06 19:52:50.852  I/System.out:    _id=1
2021-10-06 19:52:50.852  I/System.out:    table1_name=TESTOTHER
2021-10-06 19:52:50.852  I/System.out: }
2021-10-06 19:52:50.852  I/System.out: 1 {
2021-10-06 19:52:50.852  I/System.out:    _id=2
2021-10-06 19:52:50.852  I/System.out:    table1_name=TESTOTHER_ACTIVITY2
2021-10-06 19:52:50.852  I/System.out: }
2021-10-06 19:52:50.852  I/System.out: <<<<<
2021-10-06 19:52:50.856  D/ACTIVITY2INFO: Restarting Activity. counter is 0 a.a.so69454766kotlinsqlite.Activity2@3269902
2021-10-06 19:52:50.864  D/ACTIVITY2INFO: Finishing Activity 2 a.a.so69454766kotlinsqlite.Activity2@3269902
2021-10-06 19:52:51.020  D/ACTIVITY2INFO: Activity 2 Started (onCreate). a.a.so69454766kotlinsqlite.Activity2@7db7a75
2021-10-06 19:52:51.020  D/DBHELPERINFO: getInstance returning instance a.a.so69454766kotlinsqlite.DBHelper@59860ab
2021-10-06 19:52:51.021  I/System.out: >>>>> Dumping cursor android.database.sqlite.SQLiteCursor@7fc80ae
2021-10-06 19:52:51.021  I/System.out: 0 {
2021-10-06 19:52:51.021  I/System.out:    _id=1
2021-10-06 19:52:51.021  I/System.out:    table1_name=TESTOTHER
2021-10-06 19:52:51.021  I/System.out: }
2021-10-06 19:52:51.021  I/System.out: 1 {
2021-10-06 19:52:51.021  I/System.out:    _id=2
2021-10-06 19:52:51.022  I/System.out:    table1_name=TESTOTHER_ACTIVITY2
2021-10-06 19:52:51.022  I/System.out: }
2021-10-06 19:52:51.022  I/System.out: 2 {
2021-10-06 19:52:51.022  I/System.out:    _id=3
2021-10-06 19:52:51.022  I/System.out:    table1_name=TESTOTHER_ACTIVITY2
2021-10-06 19:52:51.022  I/System.out: }
2021-10-06 19:52:51.022  I/System.out: <<<<<
2021-10-06 19:52:51.023  D/ACTIVITY2INFO: Restarting Activity. counter is 1 a.a.so69454766kotlinsqlite.Activity2@7db7a75
2021-10-06 19:52:51.030  D/ACTIVITY2INFO: Finishing Activity 2 a.a.so69454766kotlinsqlite.Activity2@7db7a75
2021-10-06 19:52:51.081  D/ACTIVITY2INFO: Activity 2 Started (onCreate). a.a.so69454766kotlinsqlite.Activity2@3433874
2021-10-06 19:52:51.081  D/DBHELPERINFO: getInstance returning instance a.a.so69454766kotlinsqlite.DBHelper@59860ab
2021-10-06 19:52:51.082  I/System.out: >>>>> Dumping cursor android.database.sqlite.SQLiteCursor@5c713d1
2021-10-06 19:52:51.082  I/System.out: 0 {
2021-10-06 19:52:51.082  I/System.out:    _id=1
2021-10-06 19:52:51.082  I/System.out:    table1_name=TESTOTHER
2021-10-06 19:52:51.082  I/System.out: }
2021-10-06 19:52:51.082  I/System.out: 1 {
2021-10-06 19:52:51.083  I/System.out:    _id=2
2021-10-06 19:52:51.083  I/System.out:    table1_name=TESTOTHER_ACTIVITY2
2021-10-06 19:52:51.083  I/System.out: }
2021-10-06 19:52:51.083  I/System.out: 2 {
2021-10-06 19:52:51.083  I/System.out:    _id=3
2021-10-06 19:52:51.083  I/System.out:    table1_name=TESTOTHER_ACTIVITY2
2021-10-06 19:52:51.083  I/System.out: }
2021-10-06 19:52:51.083  I/System.out: 3 {
2021-10-06 19:52:51.083  I/System.out:    _id=4
2021-10-06 19:52:51.083  I/System.out:    table1_name=TESTOTHER_ACTIVITY2
2021-10-06 19:52:51.083  I/System.out: }
2021-10-06 19:52:51.083  I/System.out: <<<<<
2021-10-06 19:52:51.084  D/ACTIVITY2INFO: Restarting Activity. counter is 2 a.a.so69454766kotlinsqlite.Activity2@3433874
2021-10-06 19:52:51.084  D/ACTIVITY2INFO: Finishing Activity 2 a.a.so69454766kotlinsqlite.Activity2@3433874
2021-10-06 19:52:51.097  D/MAINACTIVITYINFO: On Resume invoked
2021-10-06 19:52:51.097  D/DBHELPERINFO: getInstance returning instance a.a.so69454766kotlinsqlite.DBHelper@59860ab

需要注意的一点是,检索到的 DBHelper 实例始终是 DBHelper@59860ab(即单个实例)。

如您所见,Activity 发生的情况是另一个已启动。您可能希望尝试使用注释掉的:-

        /* Perhaps the following???? */
        // this.intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION)
        //this.recreate() //<<<<<<<<<< RUN 2 perhaps the way but no intent
  • 虽然我不确定这是否会达到您想要的效果。