【问题标题】:error: Entities and Pojos must have a usable public constructor. Kotlin错误:实体和 Pojos 必须有一个可用的公共构造函数。科特林
【发布时间】:2019-11-03 01:39:59
【问题描述】:

我有以下课程:

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 必须有一个可用的公共构造函数。你可以有一个空的构造函数或参数匹配的构造函数 字段(按名称和类型)。

public final class Movie {
             ^

我查看了关于同一问题的先前问题(尤其是this),据我了解,我的问题在构造函数中的这一行:

this.movieId?.slug = slugId 

名称不匹配。但是没有机会匹配这个名字,如我所见,那么,我该如何解决这个问题?

【问题讨论】:

标签: java android kotlin retrofit2 android-room


【解决方案1】:

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

【讨论】:

  • 非常感谢!可能我仍然无法理解它是如何工作的,因为当我向 db 添加数据时,我使用的不是空的构造函数,但感谢你让它工作:)
【解决方案2】:

所以你不需要为所有实体手动添加空构造函数,你可以使用a compiler plugin

plugins {
  id "org.jetbrains.kotlin.plugin.noarg" version "1.3.31"
}

noArg {
    annotation("androidx.room.Entity")
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-11-20
    • 2019-05-21
    • 1970-01-01
    • 2019-09-14
    • 2017-11-13
    • 2018-10-19
    • 1970-01-01
    • 2018-03-17
    相关资源
    最近更新 更多