房间持久性:错误:实体和Pojos必须具有可用的公共构造函数 [英] Room Persistence: Error:Entities and Pojos must have a usable public constructor

查看:464
本文介绍了房间持久性:错误:实体和Pojos必须具有可用的公共构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将一个项目转换为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

实体和Pojos必须具有可用的公共构造函数.您可以有一个空的构造函数或一个其参数与字段匹配的构造函数(按名称和类型). 找不到字段的设置器.

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?

推荐答案

以前也有类似的问题.

首先,我已经更新/添加了apply plugin: 'kotlin-kapt'以进行升级.

First I've updated/added apply plugin: 'kotlin-kapt' to gradle.

接下来,我在gradle中使用它代替了annotationProcessor:

Next, I've used it instead of annotationProcessor in gradle:

kapt "android.arch.persistence.room:compiler:1.0.0-alpha4"

最后一件事是创建一个不可变的数据类:

Tha last thing was to create an immutable data class:

@Entity(tableName = "movies")
data class MovieKt(
    @PrimaryKey
    val id : Int,
    val title: String,
    val overview: String,
    val poster_path: String,
    val backdrop_path: String,
    val release_date: String,
    val vote_average: Double,
    val isFavorite: Int
)

更新:

当在同一Android模块中具有模型的类和数据库的类时,此解决方案有效.如果您在Android库模块中有模型类,而在主模块中有其余代码,则Room将无法识别它们.

This solution works when you have classes for the model and classes for Database in the same Android Module. If you have model classes in Android Library module and the rest of the code in your main module, Room will NOT recognize them.

这篇关于房间持久性:错误:实体和Pojos必须具有可用的公共构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆