Android 可观察房间实体(kotlin)
我正在尝试使房间实体可观察(extend BaseObservable),这样它就可以在具有双向绑定的LiveData中使用。我有一个数据类:Android 可观察房间实体(kotlin),android,kotlin,data-binding,android-room,Android,Kotlin,Data Binding,Android Room,我正在尝试使房间实体可观察(extend BaseObservable),这样它就可以在具有双向绑定的LiveData中使用。我有一个数据类: @Entity data class Model( @PrimaryKey(autoGenerate = true) val id: Int, val name: String) : Serializable 在XML中,我想这样使用它: <EditText ... android:text=&q
@Entity
data class Model(
@PrimaryKey(autoGenerate = true)
val id: Int,
val name: String) : Serializable
在XML中,我想这样使用它:
<EditText
...
android:text="@{viewModel.myLiveData.name}"
这不起作用,并导致:
> A failure occurred while executing org.jetbrains.kotlin.gradle.internal.KaptExecution
> java.lang.reflect.InvocationTargetException (no error message)
回答我自己的问题。。。 所以我的问题遗漏了一个重要的细节——在我的模型类中,我有第二个构造函数没有用@Ignore注释,所以Android Room抱怨“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
令人失望的是,它没有提到第二个构造函数 你能把整个错误日志都贴出来吗?这就是我得到的所有错误
@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