【发布时间】: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
名称不匹配。但是没有机会匹配这个名字,如我所见,那么,我该如何解决这个问题?
【问题讨论】:
-
@NileshRathod,是的,我已经检查过了
标签: java android kotlin retrofit2 android-room