最佳答案
我正在整合 Room 持久性库,我在 Kotlin 有一个数据类,比如:
@Entity(tableName = "story")
data class Story (
@PrimaryKey val id: Long,
val by: String,
val descendants: Int,
val score: Int,
val time: Long,
val title: String,
val type: String,
val url: String
)
@Entity
和 @PrimaryKey
注释是用于 Room 库的。当我尝试构建时,它错误地失败了:
Error:Cannot find setter for field.
Error:Execution failed for task ':app:compileDebugJavaWithJavac'.
> Compilation failed; see the compiler error output for details.
我还试图提供一个缺省构造函数:
@Entity(tableName = "story")
data class Story (
@PrimaryKey val id: Long,
val by: String,
val descendants: Int,
val score: Int,
val time: Long,
val title: String,
val type: String,
val url: String
) {
constructor() : this(0, "", 0, 0, 0, "", "", "")
}
但这个不太管用。需要注意的是,如果我将这个 Kotlin 类转换为一个带有 getters 和 setter 的 Java 类,那么它就可以工作了。感谢你们的帮助!