首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >错误:实体和Pojos必须有一个可用的公共构造函数。Kotlin

错误:实体和Pojos必须有一个可用的公共构造函数。Kotlin
EN

Stack Overflow用户
提问于 2019-06-20 09:00:54
回答 2查看 850关注 0票数 0

我有以下课程:

代码语言:javascript
复制
package com.mikhailovskii.trakttv.data.entity

import androidx.room.Entity
import androidx.room.Ignore
import androidx.room.PrimaryKey
import androidx.room.TypeConverters

import com.google.gson.annotations.Expose
import com.google.gson.annotations.SerializedName
import com.mikhailovskii.trakttv.db.room.MovieIdConverter

@Entity
class Movie {

    @PrimaryKey(autoGenerate = true)
    var id: Int = 0

    @SerializedName("ids")
    @Expose
    @TypeConverters(MovieIdConverter::class)
    var movieId: MovieId? = null

    @SerializedName("title")
    @Expose
    var name: String? = null

    @SerializedName("year")
    @Expose
    var year: Int = 0

    @SerializedName("tagline")
    @Expose
    var tagline: String? = null

    @SerializedName("released")
    @Expose
    var released: String? = null

    @SerializedName("runtime")
    @Expose
    var runtime: Int = 0

    @SerializedName("country")
    @Expose
    var country: String? = null

    @SerializedName("overview")
    @Expose
    var overview: String? = null

    var iconUrl: String? = null

    var watchers: Int = 0

    //For movie list
    @Ignore
    constructor(iconUrl: String, name: String, year: Int, slugId: String, watchers: Int) {
        this.iconUrl = iconUrl
        this.name = name
        this.year = year
        this.movieId?.slug = slugId
        this.watchers = watchers
    }

    //For movie detail
    @Ignore
    constructor(iconUrl: String, name: String, year: Int, tagline: String, released: String, runtime: Int, country: String, overview: String, slugId: String, watchers: Int) {
        this.iconUrl = iconUrl
        this.name = name
        this.year = year
        this.tagline = tagline
        this.released = released
        this.runtime = runtime
        this.country = country
        this.overview = overview
        this.movieId?.slug = slugId
        this.watchers = watchers
    }

    //For room
    constructor(name: String, watchers: Int, iconUrl: String, slugId: String) {
        this.iconUrl = iconUrl
        this.movieId?.slug = slugId
        this.watchers = watchers
        this.name = name
    }

}

这个类用于几个目标,例如解析JSON对象、向Room DB添加数据以及仅用于创建对象。我面临着以下问题:

错误:实体和Pojos必须有一个可用的公共构造函数。您可以拥有一个空的构造函数或一个参数与字段匹配的构造函数(按名称和类型)。

代码语言:javascript
复制
public final class Movie {
             ^

我查看了前面关于同一个问题(特别是)的问题,据我了解,我的问题在构造函数中是这样的:

代码语言:javascript
复制
this.movieId?.slug = slugId 

名字不匹配。但是,正如我所看到的,没有机会匹配这个名字,所以,我如何解决这个问题?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-06-20 09:25:23

您需要空构造函数或所有参数构造函数。因此,如果添加constructor(),问题就解决了

票数 1
EN

Stack Overflow用户

发布于 2019-06-20 13:48:20

因此,不需要手动向所有实体添加空构造函数,可以使用编译器插件

代码语言:javascript
复制
plugins {
  id "org.jetbrains.kotlin.plugin.noarg" version "1.3.31"
}

noArg {
    annotation("androidx.room.Entity")
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56682508

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档