gpt4 book ai didi

android - 房间持久性 : Error:Entities and Pojos must have a usable public constructor

转载 作者:IT老高 更新时间:2023-10-28 13:26:45 26 4
gpt4 key购买 nike

我正在将一个项目转换为 Kotlin,并且我正在尝试使我的模型(也是我的实体)成为一个数据类我打算使用 Moshi 转换来自 API 的 JSON 响应

@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
)

由于以下错误,我无法构建应用程序

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.

我找到的例子离this不远了

关于如何解决的想法?

最佳答案

在您的情况下这不是问题,但对于其他人来说,如果您的主构造函数中有 @Ignore 参数,则可能会发生此错误,即 Room 期望有:

  • 无参数构造函数或
  • 所有字段都没有用@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)

关于android - 房间持久性 : Error:Entities and Pojos must have a usable public constructor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44485631/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com