【问题标题】:Type mismatch: inferred type is AutoMigration but KClass<*> was expected类型不匹配:推断类型为 AutoMigration 但应为 KClass<*>
【发布时间】:2021-09-15 09:41:18
【问题描述】:

我试图实现一个简单的自动迁移,它只是将一个可为空的字符串字段添加到我唯一的表中,但由于某种原因,我在autoMigrations = [AutoMigration(from = 1, to = 2)] 行中收到以下错误:

类型不匹配:推断类型是 AutoMigration 但 KClass 是预期的

我说这是一个奇怪的错误,因为即使是documentation has it this way.

完整代码如下:

@Database(
    version = 2,
    entities = [Note::class],
    autoMigrations = [AutoMigration(from = 1, to = 2)]
)
@TypeConverters(Converters::class)
abstract class NotesDB : RoomDatabase() {
    abstract fun noteDao(): NoteDao

    companion object {
        @Volatile
        private var INSTANCE: NotesDB? = null

        fun getDB(context: Context): NotesDB {
            return INSTANCE ?: synchronized(this) {
                val instance = Room.databaseBuilder(
                    context.applicationContext,
                    NotesDB::class.java,
                    "notesDB"
                ).build()
                INSTANCE = instance
                instance
            }
        }
    }
}

【问题讨论】:

  • 这很有用,我在同一行中还遇到另一个错误,我认为这与第一个错误有关:Database.autoMigrations 只能从同一个库组中调用(groupId=androidx .room)
  • 考虑到我遇到了同样的错误,并且这个错误导致我只是到这个页面,我想离开它可能是一个新的错误,或者应该有一些我们不满足的其他要求。 ..
  • @AKTanara 我不得不使用手动迁移。

标签: android kotlin android-room


【解决方案1】:

将 build.gradle 中的房间版本更改为最新版本(目前为 2.4.0-alpha04)。那么如果你会得到一个关于 room.schemaLocation 的错误,看看这个答案https://stackoverflow.com/a/44424908/6568162

【讨论】:

  • 将房间依赖版本更改为2.4.0-alpha04后工作
猜你喜欢
  • 2020-04-24
  • 2020-11-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-26
相关资源
最近更新 更多