房间持久性:错误:实体和 Pojo 必须有一个可用的公共构造函数 [英] Room Persistence: Error:Entities and Pojos must have a usable public constructor
问题描述
我正在将一个项目转换为 Kotlin 并且我正在尝试将我的模型(这也是我的实体)变成一个数据类我打算使用 Moshi 来转换来自 API 的 JSON 响应
I'm converting a project to Kotlin and I'm trying to make my model (which is also my entity) a data class I intend to use Moshi to convert the JSON responses from the API
@Entity(tableName = "movies")
data class MovieKt(
@PrimaryKey
var id : Int,
var title: String,
var overview: String,
var poster_path: String,
var backdrop_path: String,
var release_date: String,
var vote_average: Double,
var isFavorite: Int
)
我无法构建应用程序导致以下错误
I can't build the app cause of the following error
实体和 Pojo 必须有一个可用的公共构造函数.您可以拥有一个空构造函数或一个其参数与字段(按名称和类型)匹配的构造函数.找不到字段的 setter.
Entities and Pojos must have a usable public constructor. You can have an empty constructor or a constructor whose parameters match the fields (by name and type). Cannot find setter for field.
我发现的例子离这个
关于如何解决它的想法?
Ideas on how to solve it?
推荐答案
这在你的情况下不是问题,但对于其他人来说,如果你的主构造函数中有 @Ignore 参数,即 Room 期望有任何一个:
It's not a problem in your case, but for others, this error can occur if you have @Ignore params in your primary constructor, i.e. Room expects to have either:
- 无参数构造函数或
- 所有字段都没有用@Ignore 标记的构造函数
例如:
@Entity(tableName = "movies")
data class MovieKt(
@PrimaryKey
var id : Int,
var title: String,
@Ignore var overview: String)
不会工作.这将:
@Entity(tableName = "movies")
data class MovieKt(
@PrimaryKey
var id : Int,
var title: String)
这篇关于房间持久性:错误:实体和 Pojo 必须有一个可用的公共构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!