为什么 ormlite 会出现错误 [java.lang.IllegalStateException: attempt to re-open an already-closed object]?

Why ormlite occur error [java.lang.IllegalStateException: attempt to re-open an already-closed object]?

这是数据库助手的代码,我使用 ORMLite:

object DatabaseHelper : OrmLiteSqliteOpenHelper(App.instance, "test.db", null, 1) {

    override fun onCreate(database: SQLiteDatabase?, connectionSource: ConnectionSource?) {            
        TableUtils.createTableIfNotExists(connectionSource, M_Data::class.java)
    }

    override fun onUpgrade(database: SQLiteDatabase?, connectionSource: ConnectionSource?, oldVersion: Int, newVersion: Int) {

            TableUtils.dropTable<M_Data, Any>(connectionSource, M_Data::class.java, true)
            onCreate(database, connectionSource)    
    }

}

这是DAO的代码:

@DatabaseTable(tableName = "m_data")
data class M_Data(

        @DatabaseField(generatedId = true)
        var id: Int? = null,

        @DatabaseField
        var username: String="",

        @DatabaseField
        var insert_date: String = ""
)
class M_DataDao
{
    companion object {
        lateinit var dao: Dao<M_Data, Int>
    }

    init {
        dao = DatabaseHelper.getDao(M_Data::class.java)
    }

    fun add(table: M_Data) = dao.createOrUpdate(table)

    fun update(table: M_Data) = dao.update(table)

    fun delete(table: M_Data) = dao.delete(table)

    fun queryForAll() = dao.queryForAll()

    fun removeAll() {
        for (table in queryForAll()) {
            dao.delete(table)
        }
    }    
     fun getListUserAddData(username:String):List<Array<String>>
{
     val sql="select username from  M_Data"
    val rawResults = dao.queryRaw(sql)

    val results = rawResults.getResults()
    return results
}
}

在我的 Activity 中,我处理获取所有数据:

val data= M_DataDao()
var listData=data.getListUserAddData("user")

退出时我关闭了连接:

override fun onDestroy() {
        super.onDestroy()
        DatabaseHelper.close()
    }

步骤发生错误:

第 1 步:打开应用程序并获取数据:确定。

第 2 步:关闭应用程序

第三步:再次打开App:出现异常:

Process: com.example.son.kotlinandroiddemo, PID: 14642
                  java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.son.kotlinandroiddemo/com.example.son.kotlinandroiddemo.ListData}: java.lang.IllegalStateException: attempt to re-open an already-closed object: SQLiteDatabase: /data/user/0/com.example.son.kotlinandroiddemo/databases/test.db
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
                      at android.app.ActivityThread.-wrap11(Unknown Source:0)
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
                      at android.os.Handler.dispatchMessage(Handler.java:106)
                      at android.os.Looper.loop(Looper.java:164)
                      at android.app.ActivityThread.main(ActivityThread.java:6494)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
                   Caused by: java.lang.IllegalStateException: attempt to re-open an already-closed object: SQLiteDatabase: /data/user/0/com.example.son.kotlinandroiddemo/databases/test.db
                      at android.database.sqlite.SQLiteClosable.acquireReference(SQLiteClosable.java:55)
                      at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1388)
                      at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1331)
                      at com.j256.ormlite.android.compat.JellyBeanApiCompatibility.rawQuery(JellyBeanApiCompatibility.java:21)
                      at com.j256.ormlite.android.AndroidCompiledStatement.getCursor(AndroidCompiledStatement.java:193)
                      at com.j256.ormlite.android.AndroidCompiledStatement.runQuery(AndroidCompiledStatement.java:72)
                      at com.j256.ormlite.stmt.SelectIterator.<init>(SelectIterator.java:57)
                      at com.j256.ormlite.stmt.RawResultsImpl.<init>(RawResultsImpl.java:30)
                      at com.j256.ormlite.stmt.StatementExecutor.queryRaw(StatementExecutor.java:276)
                      at com.j256.ormlite.dao.BaseDaoImpl.queryRaw(BaseDaoImpl.java:619)
                      at com.example.son.kotlinandroiddemo.db.dao.M_DataDao.getListUserAddData(M_Data.kt:49)
                      at com.example.son.kotlinandroiddemo.ListData.generateData(ListData.kt:35)
                      at com.example.son.kotlinandroiddemo.ListData.onCreate(ListData.kt:28)
                      at android.app.Activity.performCreate(Activity.java:6999)
                      at android.app.Activity.performCreate(Activity.java:6990)
                      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856) 
                      at android.app.ActivityThread.-wrap11(Unknown Source:0) 
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589) 
                      at android.os.Handler.dispatchMessage(Handler.java:106) 
                      at android.os.Looper.loop(Looper.java:164) 
                      at android.app.ActivityThread.main(ActivityThread.java:6494) 

为什么 ormlite 会出现错误 [java.lang.IllegalStateException: 试图重新打开一个已经关闭的对象]? 如何修复此错误? 谢谢大家

我通过删除函数 onDestroy 中的 DatabaseHelper.close() 修复了这个错误。