【问题标题】:Error using @Embedded Tag when setting up Room Database设置房间数据库时使用 @Embedded 标签时出错
【发布时间】:2020-09-21 13:54:25
【问题描述】:

我正在尝试在数据类中使用@Embedded,以便将子字段保存为我的房间数据库中的列。当我尝试运行我的应用程序时,出现此错误:

Expected BEGIN_OBJECT but was BEGIN_ARRAY

我的数据类如下所示:

@Entity(
    tableName = "workorder",
    indices = [Index("order_number"), Index("customer_number")]
)
data class WorkorderResponse(

    @ColumnInfo(name = "acres")
    val acres: Double?,
    @PrimaryKey
    @ColumnInfo(name = "order_number")
    val orderNumber: Int,
   ...
    @Embedded(prefix="application_")
    val orderApplication: OrderApplication,
)

订单应用程序类如下所示:

data class OrderApplication(
    val acres: String?,
    val applicationMethod: String?,
    val carrier: String?,
    val cropHeight: Int?,
    val currentPrecip: Int?,
    val fieldConditions: String?,
    val loadSiteLat: Int?,
    val loadSiteLong: Int?,
    val loadingMethod: String?,
    val machine: String?,
    val method: String?,
    val nextDayPrecipAmount: Int?,
    val nextDayPrecipChance: Int?,
    val nextDayTemperature: Int?,
    val nextDayWeather: String?,
    val notes: String?,
    val notified: String?,
    val `operator`: Int?,
    val pressure: String?,
    val previousOrder: String?,
    val reEntryInterval: Int?,
    val rinsedPost: String?,
    val rinsedPrevious: String?,
    val rotateRest: String?,
    val sensitiveArea: String?,
    val sequence: Int?,
    val soilConditions: String?,
    val soilType: String?,
    val temperature: Int?,
    val tenderOperator: String?,
    val timestampBegin: String?,
    val timestampEnd: String?,
    val tipSize: String?,
    val weather: String?,
    val weedHeight: Int?,
    val windDirection: String?,
    val windSpeed: Int?,
    val wkt: String?
)

为了正确设置,我还需要做些什么吗? 提前致谢!

我尝试过使用这样的列表:

    @Embedded(prefix="application_")
    val orderApplication: List<OrderApplication>,

但这给了我这个错误:

Entities and POJOs must have a usable public constructor.

另外,我有这些类型转换器:

    @TypeConverter
    fun fromOrderApplication(orderApplication: List<OrderApplication>): String{
        val type = object : TypeToken<List<OrderApplication>>() {}.type
        return gson.toJson(orderApplication, type)
    }

    @TypeConverter
    fun toOrderApplication(json: String): List<OrderApplication> {
        val type = object : TypeToken<List<OrderApplication>>() {}.type
        return gson.fromJson(json, type)
    }

【问题讨论】:

    标签: android android-studio kotlin android-room room


    【解决方案1】:

    我从来不知道如何让@Embedded 工作。我最终只使用类型转换器将 OrderApplication 数据作为 JSON 字符串存储在一列中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多