当前位置: 首页 > 知识库问答 >
问题:

房间持久性库:尝试重新打开已关闭的对象

松增
2023-03-14

java.lang.IllegalStateException:尝试重新打开已关闭的对象:SQLiteDatabase:/data/data/package_name/databases/app_db

我该怎么解决这个?

编辑:

@Singleton
public class DatabaseHelper {

private final AppDatabase appDatabase;

@Inject
public DatabaseHelper(@ApplicationContext Context context) {
    appDatabase = Room.databaseBuilder(context, AppDatabase.class, "app_db").build();        
}

public List<DifficultyEntity> getAllDifficulties() {
        return appDatabase.difficultyDao().getAllDifficulties();
}
@Database(version = 22, entities = {
        DifficultyEntity.class, // ....
})
@Singleton
public abstract class AppDatabase extends RoomDatabase {

    public abstract DifficultyDao difficultyDao();
    //....
}

当我调用GetAllDowleds方法时,我得到了这个错误。

以下是logcat:

java.lang.IllegalStateException: attempt to re-open an already-closed object: SQLiteDatabase: /data/data/package_name/databases/app_db
       at android.database.sqlite.SQLiteClosable.acquireReference(SQLiteClosable.java:55)
       at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1783)
       at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1729)
       at android.arch.persistence.db.framework.FrameworkSQLiteDatabase.execSQL(FrameworkSQLiteDatabase.java:241)
       at android.arch.persistence.room.RoomOpenHelper.createMasterTableIfNotExists(RoomOpenHelper.java:130)
       at android.arch.persistence.room.RoomOpenHelper.checkIdentity(RoomOpenHelper.java:106)
       at android.arch.persistence.room.RoomOpenHelper.onOpen(RoomOpenHelper.java:99)
       at android.arch.persistence.db.framework.FrameworkSQLiteOpenHelper$1.onOpen(FrameworkSQLiteOpenHelper.java:64)
       at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:266)
       at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163)
       at android.arch.persistence.db.framework.FrameworkSQLiteOpenHelper$OpenHelper.getWritableSupportDatabase(FrameworkSQLiteOpenHelper.java:106)
       at android.arch.persistence.db.framework.FrameworkSQLiteOpenHelper.getWritableDatabase(FrameworkSQLiteOpenHelper.java:82)
       at android.arch.persistence.room.RoomDatabase.query(RoomDatabase.java:191)
       at uz.iumarov.ieltsspeaking.data.local.database.daos.RecordingDao_Impl.getAllRecordings(RecordingDao_Impl.java:167)
       at uz.iumarov.ieltsspeaking.data.local.database.DatabaseHelper.getAllRecording(DatabaseHelper.java:361)
       at uz.iumarov.ieltsspeaking.ui.main.recordingsFragment.RecordingsPresenter$1.call(RecordingsPresenter.java:77)
       at uz.iumarov.ieltsspeaking.ui.main.recordingsFragment.RecordingsPresenter$1.call(RecordingsPresenter.java:74)
       at rx.internal.operators.OnSubscribeDefer.call(OnSubscribeDefer.java:46)
       at rx.internal.operators.OnSubscribeDefer.call(OnSubscribeDefer.java:35)
       at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:48)
       at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:30)
       at rx.Observable.unsafeSubscribe(Observable.java:10151)
       at rx.internal.operators.OnSubscribeMap.call(OnSubscribeMap.java:48)
       at rx.internal.operators.OnSubscribeMap.call(OnSubscribeMap.java:33)
       at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:48)
       at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:30)
       at rx.Observable.unsafeSubscribe(Observable.java:10151)
       at rx.internal.operators.OperatorSubscribeOn$1.call(OperatorSubscribeOn.java:94)
       at rx.internal.schedulers.EventLoopsScheduler$EventLoopWorker$1.call(EventLoopsScheduler.java:172)
       at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:55)
       at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:422)
       at java.util.concurrent.FutureTask.run(FutureTask.java:237)
       at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:152)
       at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:265)
       at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
       at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
       at java.lang.Thread.run(Thread.java:818)

编辑2:

java.lang.IllegalStateException: Room cannot verify the data integrity. Looks like you've changed schema but forgot to update the version number. You can simply fix this by increasing the version number.
       at android.arch.persistence.room.RoomOpenHelper.checkIdentity(RoomOpenHelper.java:118)
       at android.arch.persistence.room.RoomOpenHelper.onOpen(RoomOpenHelper.java:99)
       at android.arch.persistence.db.framework.FrameworkSQLiteOpenHelper$1.onOpen(FrameworkSQLiteOpenHelper.java:64)
       at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:266)
       at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163)
       at android.arch.persistence.db.framework.FrameworkSQLiteOpenHelper$OpenHelper.getWritableSupportDatabase(FrameworkSQLiteOpenHelper.java:106)
       at android.arch.persistence.db.framework.FrameworkSQLiteOpenHelper.getWritableDatabase(FrameworkSQLiteOpenHelper.java:82)
       at android.arch.persistence.room.RoomDatabase.query(RoomDatabase.java:191)
       at uz.iumarov.ieltsspeaking.data.local.database.daos.RecordingDao_Impl.getAllRecordings(RecordingDao_Impl.java:167)
       at uz.iumarov.ieltsspeaking.data.local.database.DatabaseHelper.getAllRecording(DatabaseHelper.java:361)
       at uz.iumarov.ieltsspeaking.ui.main.recordingsFragment.RecordingsPresenter$1.call(RecordingsPresenter.java:77)
       at uz.iumarov.ieltsspeaking.ui.main.recordingsFragment.RecordingsPresenter$1.call(RecordingsPresenter.java:74)
       at rx.internal.operators.OnSubscribeDefer.call(OnSubscribeDefer.java:46)
       at rx.internal.operators.OnSubscribeDefer.call(OnSubscribeDefer.java:35)
       at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:48)
       at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:30)
       at rx.Observable.unsafeSubscribe(Observable.java:10151)
       at rx.internal.operators.OnSubscribeMap.call(OnSubscribeMap.java:48)
       at rx.internal.operators.OnSubscribeMap.call(OnSubscribeMap.java:33)
       at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:48)
       at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:30)
       at rx.Observable.unsafeSubscribe(Observable.java:10151)
       at rx.internal.operators.OperatorSubscribeOn$1.call(OperatorSubscribeOn.java:94)
       at rx.internal.schedulers.EventLoopsScheduler$EventLoopWorker$1.call(EventLoopsScheduler.java:172)
       at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:55)
       at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:422)
       at java.util.concurrent.FutureTask.run(FutureTask.java:237)
       at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:152)
       at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:265)
       at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
       at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
       at java.lang.Thread.run(Thread.java:818)
java.lang.IllegalStateException: A migration from 22 to 23 is necessary. Please provide a Migration in the builder or call fallbackToDestructiveMigration in the builder in which case Room will re-create all of the tables.
       at android.arch.persistence.room.RoomOpenHelper.onUpgrade(RoomOpenHelper.java:81)
       at android.arch.persistence.db.framework.FrameworkSQLiteOpenHelper$1.onUpgrade(FrameworkSQLiteOpenHelper.java:49)
       at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:256)
       at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163)
       at android.arch.persistence.db.framework.FrameworkSQLiteOpenHelper$OpenHelper.getWritableSupportDatabase(FrameworkSQLiteOpenHelper.java:106)
       at android.arch.persistence.db.framework.FrameworkSQLiteOpenHelper.getWritableDatabase(FrameworkSQLiteOpenHelper.java:82)
       at android.arch.persistence.room.RoomDatabase.query(RoomDatabase.java:191)
       at uz.iumarov.ieltsspeaking.data.local.database.daos.RecordingDao_Impl.getAllRecordings(RecordingDao_Impl.java:167)
       at uz.iumarov.ieltsspeaking.data.local.database.DatabaseHelper.getAllRecording(DatabaseHelper.java:361)
       at uz.iumarov.ieltsspeaking.ui.main.recordingsFragment.RecordingsPresenter$1.call(RecordingsPresenter.java:77)
       at uz.iumarov.ieltsspeaking.ui.main.recordingsFragment.RecordingsPresenter$1.call(RecordingsPresenter.java:74)
       at rx.internal.operators.OnSubscribeDefer.call(OnSubscribeDefer.java:46)
       at rx.internal.operators.OnSubscribeDefer.call(OnSubscribeDefer.java:35)
       at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:48)
       at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:30)
       at rx.Observable.unsafeSubscribe(Observable.java:10151)
       at rx.internal.operators.OnSubscribeMap.call(OnSubscribeMap.java:48)
       at rx.internal.operators.OnSubscribeMap.call(OnSubscribeMap.java:33)
       at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:48)
       at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:30)
       at rx.Observable.unsafeSubscribe(Observable.java:10151)
       at rx.internal.operators.OperatorSubscribeOn$1.call(OperatorSubscribeOn.java:94)
       at rx.internal.schedulers.EventLoopsScheduler$EventLoopWorker$1.call(EventLoopsScheduler.java:172)
       at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:55)
       at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:422)
       at java.util.concurrent.FutureTask.run(FutureTask.java:237)
       at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:152)
       at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:265)
       at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
       at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
       at java.lang.Thread.run(Thread.java:818)

我删除了我的应用程序并重新安装。现在,alpha9 Verison的一切工作都很好。

共有1个答案

韦泳
2023-03-14

你必须提供迁移。在Live应用中,你不能告诉每个人请删除应用程序和重新安装应用程序

在Room新版本1.0.0-alpha9中,Room增加了对NOT NULL约束的支持。

这将改变Room生成的模式。因为它改变了模式,所以它也改变了DB的identityHash,Room使用它唯一地标识每个DB版本。因此,我们需要一个迁移

 类似资料:
  • 问题内容: 当我尝试在Project Properties中将项目构建目标更改为4.1 google API时,在logcat中收到这些错误。我试图修复项目属性并清理项目,但没有成功。有什么问题? 问题答案: 确保您仅使用的一个实例。我猜想您实例化了多个实例,结果Android抱怨了。 您也应该查看有关此主题的 博客文章 。

  • Android的Room持久性库优雅地包含了适用于对象或集合的@Insert和@Update注释。但是,我有一个用例(包含模型的推送通知),它需要UPSERT,因为数据库中可能存在数据,也可能不存在数据。 Sqlite本机不具有up,在这个SO问题中描述了解决方法。鉴于那里的解决方案,如何将它们应用于Room? 更具体地说,如何在房间中实现插入或更新,而不破坏任何外键约束?使用insert wit

  • Android等级插件版本:7.0.4 Gradle版本:7.3.3 Gradle JDK:11.0.10(Android Studio默认JDK) 我一直在努力寻找解决这个问题的具体方法。Android Room kot Lin Kapt IntelliJ MAC OS Catalina:Java . lang . unsatisfiedlinkerror:org . SQLite . core

  • 转换器.KT FoodDatabase.kt 当我运行以下代码创建数据库时: 我还附上我的评分文件: 有人遇到过这个问题吗?

  • 最近几天,我一直在学习新的Android架构组件。跟进一些博文后,留档

  • 我刚刚使用Android room persistence library,通常我使用DBHeper来管理数据,但我正在尝试使用这项技术,我想知道如何在room persistence library中使用这个查询 但是,当我在“喜欢”之后使用%时,我会在红线上划下它