我正在與房間持久性庫集成。我喜歡在科特林數據類:無法找到填充字段 - 使用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
註解是爲客房庫。當我嘗試建立,它與錯誤而失敗:
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類轉換爲帶有getter和setter的Java類,它就可以工作。任何幫助表示讚賞!
在https://github.com/googlesamples/android-architecture-components/blob/master/BasicRxJavaSampleKotlin/app/src/main/java/com/example /goroid/observability/persistence/User.kt從谷歌的例子中,不可變屬性沒有任何問題。有人可以分析原因嗎?它可能是一個錯誤? –