Kotlin 结合 Room 报错 There are multiple good constructors and Room will pick the no-arg constructor.

创建了如下一张表:

@Entity(tableName = "tasks")
data class Task @JvmOverloads constructor(
    @ColumnInfo(name = "title") var title: String = "",
    @ColumnInfo(name = "description") var description: String = "",
    @ColumnInfo(name = "completed") var isCompleted: Boolean = false,
    @PrimaryKey @ColumnInfo(name = "entryid") var id: String = UUID.randomUUID().toString()
) {

    val titleForList: String
        get() = if (title.isNotEmpty()) title else description


    val isActive
        get() = !isCompleted

    val isEmpty
        get() = title.isEmpty() || description.isEmpty()
}

执行build之后报错:

警告: There are multiple good constructors and Room will pick the no-arg constructor. You can use the @Ignore annotation to eliminate unwanted constructors.

首先尝试:

  1. 去掉@JvmOverloads
  2. 创建一个空参数 的 constructor(),在该构造函数上方添加@Ignore注解
@Entity(tableName = "tasks")
data class Task constructor(
    @ColumnInfo(name = "title") var title: String = "",
    @ColumnInfo(name = "description") var description: String = "",
    @ColumnInfo(name = "completed") var isCompleted: Boolean = false,
    @PrimaryKey @ColumnInfo(name = "entryid") var id: String = UUID.randomUUID().toString()
) {
    @Ignore
    constructor():this("","",false,"")
    val titleForList: String
        get() = if (title.isNotEmpty()) title else description


    val isActive
        get() = !isCompleted

    val isEmpty
        get() = title.isEmpty() || description.isEmpty()
}

再次执行build,报错如下:

警告: The query returns some columns [title, description, completed, entryid] which are not used by java.lang.Object. You can use @ColumnInfo annotation on the fields to specify the mapping.  You can suppress this warning by annotating the method with @SuppressWarnings(RoomWarnings.CURSOR_MISMATCH). Columns returned by the query: title, description, completed, entryid. Fields in java.lang.Object: .

此时就需要考虑下Room的版本和Kotlin的版本是不是匹配?!!
考虑下Room的版本和Kotlin的版本是不是匹配?!!
Room的版本和Kotlin的版本是不是匹配?!!

我的Room版本当前是2.1.0;Kotlin版本当前是1.6.0 ;

Kotlin-Version = 1.6.0 => Room-Version = 2.4.2
Kotlin-Version = 1.3.31 => Room-Version = 2.1.0
Kotlin-Version = 1.5.20 => Room-Version = 2.3.0

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值