创建了如下一张表:
@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.
首先尝试:
- 去掉@JvmOverloads
- 创建一个空参数 的 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