会议室:实体和Pojos必须具有可用的公共构造函数

时间:2018-07-13 12:22:11

标签: android kotlin android-room

在我的Room数据类中,我有两个构造函数。一个用于除JSON对象和一个布尔值,另一个用于除JSON对象并使用默认布尔值调用另一个。

问题是我遇到Room: Entities and Pojos must have a usable public constructor错误,Snd似乎无法确定应在此处执行的操作:

package com.myapp.android.common.database.room.entities

import android.arch.persistence.room.Entity
import android.arch.persistence.room.Ignore
import android.arch.persistence.room.PrimaryKey
import android.arch.persistence.room.TypeConverters
import com.myapp.android.common.database.room.entities.typeconverters.UserStatusConverter
import org.json.JSONObject

@Entity
@TypeConverters(UserStatusConverter::class)
data class User(
        @PrimaryKey
        var id: Long,
        var name: String,
        var pictureUrl: String,
        var country: String,
        var isPremium: Boolean,
        var mutualFriends: Int?,
        var status: Status?,
        @Ignore
        var isSearch: Boolean
) {    
    constructor(o: JSONObject) :
            this(o, false)

    constructor(o: JSONObject, isSearch: Boolean) :
            this(
                    (if (o.has("id")) o.getLong("id") else -1),
                    (if (o.has("name")) o.getString("name") else "-"),
//                    (if (o.has("email")) o.getString("email") else "-"),
                    (if (o.has("picture_url")) o.getString("picture_url") else ""),
                    (if (o.has("country")) o.getString("country") else ""),
                    (o.optBoolean("is_premium", false) || o.optBoolean("premium", false)),
                    (if (o.has("mutual_friends")) o.getInt("mutual_friends") else -1),
                    (if (o.has("status")) User.Status.toStatus(o.getString("status")) else Status.friend),
                    isSearch
            )

    enum class Status private constructor(val status: String) {
        friend("friends"),
        pending("pending_invite"),
        nofriend("not_friends");

        companion object {
            fun toStatus(status: String): Status? {
                for (type in Status.values()) {
                    if (type.status == status) {
                        return type
                    }
                }
                return null
            }
        }
    }
}

0 个答案:

没有答案