11

I have a data class that I created an Entity from that for my database.

this is my data class:

@Entity
@Parcelize
data class Tapligh(

    @PrimaryKey(autoGenerate = true)
    var id: Long,

    @SerializedName("title") var title: String?,
    @SerializedName("type") var type: Int?,
    @SerializedName("os") var os: Int?,
    @SerializedName("logo") var logo: String?,
    @SerializedName("template") var template: String?,
    @SerializedName("action") var action: String?,
    @SerializedName("date") var date: String?,
    @Embedded
    @SerializedName("videos") var videos: Videos?,

) : Parcelable {

    fun getTaplighType(): Int {

        return when (this.type) {

            0 -> TaplighType.IMAGE.type
            1 -> TaplighType.VIDEO.type
            else -> TaplighType.NATIVE.type
        }
    }
}

@Parcelize
data class Videos(

    @SerializedName("land") var land: String?,
    @SerializedName("port") var port: String?
) : Parcelable

Now by adding below field into my Tapligh data class, I will get an error:

 @Ignore
 @SerializedName("images") var images: List<String>?

I am getting this error:

error: 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).
public final class Tapligh implements android.os.Parcelable {
             ^
Ehsan
  • 2,676
  • 6
  • 29
  • 56
  • Possible duplicate of [Room Persistence: Error:Entities and Pojos must have a usable public constructor](https://stackoverflow.com/questions/44485631/room-persistence-errorentities-and-pojos-must-have-a-usable-public-constructor) Use an empty constructor. See the answer here: https://stackoverflow.com/a/54107314/2519748 – Rajarshi Jan 09 '19 at 09:52
  • Use an empty constructor. See the answer here: https://stackoverflow.com/a/54107314/2519748 – Rajarshi Jan 09 '19 at 09:54

3 Answers3

14

@Ignore in Kotlin classes requires default parameter value and @JvmOverloads annotation:

data class Tapligh  @JvmOverloads constructor(
    ...

    @Ignore
    @SerializedName("images") var images: List<String>? = null
)
gmk57
  • 839
  • 11
  • 20
0

Sometimes new field create an error when class implements Parcelable, so try to remove Parcelable and body of class, put Parcelable and implement members again or just implement Serializable interface instead of Parcelable.

0

The provided Taplight has errors:

@PrimaryKey(autoGenerate = true) var id: Long,
@SerializedName("title") var title: String?,
@SerializedName("type") var type: Int?,
@SerializedName("os") var os: Int?,
@SerializedName("logo") var logo: String?,
@SerializedName("template") var template: String?,
@SerializedName("action") var action: String?,
@SerializedName("date") var date: String?,
@Embedded
@SerializedName("videos") var videos: Videos?) {

constructor() : this(0,null,null,null,null,null,null,null,null)

videos seems like the last argument and the constructor was badly called. I don't know if that was a copy-paste or formatting error, but hope it could help.

shkschneider
  • 17,833
  • 13
  • 59
  • 112
  • the problem is not ```videos```, When I try to add ```@Ignore @SerializedName("images") var images: List?``` I will get the error – Ehsan Jan 08 '19 at 11:08
  • Then please edit your question's code as it's invalid as of now. And i'll drop my non-answer ^^ – shkschneider Jan 08 '19 at 11:11