Room Kotlin 找不到字段的设置器

Posted

技术标签:

【中文标题】Room Kotlin 找不到字段的设置器【英文标题】:Room Kotlin Cannot find setter for field 【发布时间】:2018-07-25 20:49:32 【问题描述】:

您好,我正在尝试在基于 MVP 的项目中使用带有 Kotlin 的 Room。

我的问题是,当我创建一个数据类和相关的 DAO 时,所有类都会成功生成,但是当创建第二个数据类并且它是 DAO 时,Error:Cannot find setter for field. 会抛出在 android studio 中重建命令

@Entity(tableName = "TB_CLASSES")
class TbClass(
@ColumnInfo(name = "ID")
@PrimaryKey(autoGenerate = true)  var id: Int =-1,
@ColumnInfo(name = "NAME") var name: String="",
@ColumnInfo(name = "CAPTION") var caption: String="",
@ColumnInfo(name = "TYPE") var type: String="")

@Entity(tableName = "TB_FEATURE_DISA")
class TbFeatureDisa(
@ColumnInfo(name = "ID")
@PrimaryKey(autoGenerate = true) var id: Int=-1,
@ColumnInfo(name = "FEATURE_ID") var featureId: Int=-1,
@ColumnInfo(name = "DISA_ID") var disaId: Int=-1,
@ColumnInfo(name = "IS_SOLVED") var isSolved: Int=-1,
@ColumnInfo(name = "DISA_LEVEL") var disaLevel: Double=-0.1,
@ColumnInfo(name = "RESOLVER_USER_NAME") var resolverUserName: String="",
@ColumnInfo(name = "RESOLVE_TIME") var resolveTime: Date? =null,
@ColumnInfo(name = "REPORT_VIST_ID") var reportVisitId: Int=-1,
@ColumnInfo(name = "REPORT_FAULT_ID") var reportFaultId: Int=-1,
@ColumnInfo(name = "SOLVE_VIST_ID") var solveVisitId: Int=-1,
@ColumnInfo(name = "SOLVE_SERVICE_ID") var solveServiceId: Int=-1,
@ColumnInfo(name = "SOLVE_FUALT_ID") var solveFaultId: Int=-1)

我也检查了这个link 和这个link,但没有一个对我有用

【问题讨论】:

【参考方案1】:

我终于找到了解决办法。问题与属性命名有关。

以下导致编译时出错:

 @ColumnInfo(name = "IS_SOLVED") var isSolved 

但是当我把上面的改成下面的时候,错误就解决了:

 @ColumnInfo(name = "IS_SOLVED") var solved 

我们不能使用 SQLite 保留关键字line 进行字段命名。 我使用的是 Room 1.0.0 版和 kotlin 1.1.51 版。

【讨论】:

【参考方案2】:

创建一个data class,它将为属性提供settersgetters,如

@Entity(tableName = "TB_CLASSES")
data class TbClass( //< add data prior to class
@ColumnInfo(name = "ID")
@PrimaryKey(autoGenerate = true)  var id: Int =-1,
@ColumnInfo(name = "NAME") var name: String="",
@ColumnInfo(name = "CAPTION") var caption: String="",
@ColumnInfo(name = "TYPE") var type: String="")

TbFeatureDisa

    @Entity(tableName = "TB_FEATURE_DISA")
    data class TbFeatureDisa(
    @ColumnInfo(name = "ID")
    @PrimaryKey(autoGenerate = true) var id: Int=-1,
    @ColumnInfo(name = "FEATURE_ID") var featureId: Int=-1,
    @ColumnInfo(name = "DISA_ID") var disaId: Int=-1,
    @ColumnInfo(name = "IS_SOLVED") var isSolved: Int=-1,
    @ColumnInfo(name = "DISA_LEVEL") var disaLevel: Double=-0.1,
    @ColumnInfo(name = "RESOLVER_USER_NAME") var resolverUserName: String="",
    @ColumnInfo(name = "RESOLVE_TIME") var resolveTime: Date? =null,
    @ColumnInfo(name = "REPORT_VIST_ID") var reportVisitId: Int=-1,
    @ColumnInfo(name = "REPORT_FAULT_ID") var reportFaultId: Int=-1,
    @ColumnInfo(name = "SOLVE_VIST_ID") var solveVisitId: Int=-1,
    @ColumnInfo(name = "SOLVE_SERVICE_ID") var solveServiceId: Int=-1,
    @ColumnInfo(name = "SOLVE_FUALT_ID") var solveFaultId: Int=-1)

【讨论】:

【参考方案3】:

对于像我这样缺乏关注并通过 Google 偶然发现这里的人,使用 val 而不是 var 会给出相同的错误消息。

【讨论】:

以上是关于Room Kotlin 找不到字段的设置器的主要内容,如果未能解决你的问题,请参考以下文章

Android Room 库错误:找不到字段的设置器。 (科特林)

找不到类,使用 Android Studio 3.0.1、Room、Kotlin 的 androidTest 中的空测试套件

错误:找不到字段的设置器。 - java.util.ArrayList 中的大小 - Room 中的嵌入式 ArrayList 无法编译

pycharm解析器找不到

错误:找不到字段的设置器 - 房间

错误:找不到字段的吸气剂;当数据类与 Room 一起使用时