类型不匹配:推断类型为 AutoMigration,但预期为 KClass<*>

Byt*_*Seb 10 android kotlin android-room

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

类型不匹配:推断类型为 AutoMigration,但预期为 KClass<*>

我说这是一个奇怪的错误,因为即使文档也是这样的。

完整代码如下:

@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
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

小智 4

将 build.gradle 中的房间版本更改为最新版本(目前为 2.4.0-alpha04)。然后,如果您收到有关 room.schemaLocation 的错误,请查看此答案/sf/answers/3109743591/