Android - Kotlin 下使用 Room 遇到 There are multiple good constructors and Room will ... 问题

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/LABLENET/article/details/79602377

问题:

kotlin 更新值 1.2.6 版本时,room 表的实体类就编译不通过了,比如:

@Entity(tableName = "table_hex")
data class HexData constructor(
        @ColumnInfo(name = "id")
        @PrimaryKey(autoGenerate = true)
        var id: Int = 0,
        @ColumnInfo(name = "code")
        var code: String = "",
        @ColumnInfo(name = "datas")
        var datas: String = "") {
    constructor() : this(0)
}

会出现下面警告和异常

Warning:(11, 2) 警告: There are multiple good constructors and Room will pick the no-arg construct

当新增加的字段时,就会编译失败,找不到 setter 方法,其实都是上面警告引起的

解决:

在构造函数上添加 Ignore 注解即可

@Entity(tableName = "table_hex")
data class HexData constructor(
        @ColumnInfo(name = "id")
        @PrimaryKey(autoGenerate = true)
        var id: Int = 0,
        @ColumnInfo(name = "code")
        var code: String = "",
        @ColumnInfo(name = "datas")
        var datas: String = "") {
    @Ignore
    constructor() : this(0)
}

猜你喜欢

转载自blog.csdn.net/LABLENET/article/details/79602377