【问题标题】:Observable Room entity (kotlin)可观察房间实体(kotlin)
【发布时间】:2020-11-07 19:24:59
【问题描述】:

我正在尝试使房间实体可观察(扩展 BaseObservable),以便可以在具有双向绑定的 LiveData 中使用它。所以我有这个数据类:

@Entity
data class Model(

@PrimaryKey(autoGenerate = true)
val id: Int,
val name: String) : Serializable

在 XML 中我想像这样使用它:

<EditText
            ...
            android:text="@{viewModel.myLiveData.name}"

在我的 ViewModel 中:

val myLiveData: MutableLiveData<Model>

我发现基本相同的问题here.. 但对于 JAVA,我在将其转换为 kotlin 时遇到了问题。我的 kotlin 等效项是:

@Entity
class Model: BaseObservable,  Serializable {

   constructor(id: Int, name: String) {
     this.id = id
     this.name = name
   }

  @PrimaryKey(autoGenerate = true)
  var id: Int = 0

  @get:Bindable
  var name: String = ""
    set(value) {
        field = value
        notifyChange()
    }
 }

这不起作用并导致:

> A failure occurred while executing org.jetbrains.kotlin.gradle.internal.KaptExecution
> java.lang.reflect.InvocationTargetException (no error message)

【问题讨论】:

  • 你能发布整个错误日志吗?
  • 这就是我得到的所有错误

标签: android kotlin data-binding android-room


【解决方案1】:

回答我自己的问题... 所以我的问题遗漏了一个重要的细节——在我的模型类中,我有第二个构造函数,它没有用 @Ignore 注释,所以 Android Room 抱怨“房间不能选择构造函数,因为多个构造函数是合适的。尝试用 @Ignore 注释不需要的构造函数。 "

但是(!)该错误仅针对 Java 中的类显示,而不是在 Kotlin 中,出于某种奇怪的原因。

所以在 Java 中我有这个类:

@Entity
public class Model extends BaseObservable implements Serializable {

   @PrimaryKey(autoGenerate = true)
   public int id;

   private String name;

   //@Ignore (THIS SHOULD BE UNCOMMENTED TO GET RID OF AN ERROR!!!!!
   public Model(String name) {
      this.id = 0;
      this.name = name;
   }

   public Model(int id, String name) {
      this.id = id;
      this.name = name;
   }

   public void setName(String name) {
      if (!Objects.equals(name, this.name)) {
          this.name = name;
          notifyPropertyChanged(BR.name);
      }
   }

   @Bindable
   public String getName() {
      return name;
   } 
}

所以对于这个 Java 类,我得到了:

Room cannot pick a constructor since multiple constructors are suitable. Try to annotate unwanted constructors with @Ignore.

那么对于等价的 Kotlin 类:

@Entity
class Model: BaseObservable,  Serializable {

  constructor(id: Int, name: String) {
      this.id = id
      this.name = name
  }

  // @Ignore - THIS VAS MISSING!!!
  constructor(name: String): this(0, name)

  @PrimaryKey(autoGenerate = true)
  var id: Int = 0

  @get:Bindable
  var name: String = ""
      set(value) {
          field = value
          notifyChange()
      } 
}

我只得到这个错误:

> Task :app:kaptDebugKotlin FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:kaptDebugKotlin'.
> A failure occurred while executing org.jetbrains.kotlin.gradle.internal.KaptExecution
> java.lang.reflect.InvocationTargetException (no error message)

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 2s
28 actionable tasks: 2 executed, 26 up-to-date

令人失望的是,没有提到第二个构造函数!!!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-19
    • 1970-01-01
    • 2023-04-02
    • 1970-01-01
    相关资源
    最近更新 更多