数据绑定错误 - 找不到访问器
Databinding error - Could not find accessor
我用谷歌搜索但仍然没有找到适合我的解决方案。
这是我的 xml:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<data>
<import
alias="noteViewModel"
type="com.app.screen.createnote.CreateNoteViewModel" />
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:id="@+id/etNote"
style="@style/JWidget.EditText.Grey"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="start"
android:inputType="textMultiLine"
android:scrollbars="vertical"
android:text="@={noteViewModel.note.description}" />
</LinearLayout>
</layout>
这是我的 ViewModel 代码:
public class CreateNoteViewModel extends BaseObservable {
private Note note;
@Bindable
public Note getNote() {
return note;
}
public void setNote(Note note) {
this.note = note;
notifyPropertyChanged(BR.note);
}
}
但是当我尝试 运行 我的应用程序时,我明白了:
Error:Execution failed for task ':app:compileDebugJavaWithJavac'.
java.lang.RuntimeException: Found data binding errors.
****/ data binding error ****msg:Could not find accessor com.justsafe.tmgr.justsafe.screen.createnote.CreateNoteViewModel.note
file:C:\android\work\JustSafeTmgr\app\src\main\res\layout\activity_create_note.xml
loc:44:33 - 44:62
****\ data binding error ****
P.S。在我的应用程序的其他地方它可以工作,但我遇到了问题。
尝试将 <import>
标签更改为 <variable>
标签:
<data>
<variable
name="noteViewModel"
type="com.app.screen.createnote.CreateNoteViewModel" />
</data>
build.gradle -- 应用程序
android {
dataBinding {
enabled = true
}
}
dependencies {
def life_versions = "1.1.1"
// Lifecycle components
implementation "android.arch.lifecycle:extensions:$life_versions"
annotationProcessor "android.arch.lifecycle:compiler:$life_versions"
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable
name="LoginViewModel"
type="com.keshav.mvvmdemokeshav.viewModel.LoginViewModel" />
</data>
<!-- TODO Your XML File-->
</layout>
我用谷歌搜索但仍然没有找到适合我的解决方案。
这是我的 xml:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<data>
<import
alias="noteViewModel"
type="com.app.screen.createnote.CreateNoteViewModel" />
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:id="@+id/etNote"
style="@style/JWidget.EditText.Grey"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="start"
android:inputType="textMultiLine"
android:scrollbars="vertical"
android:text="@={noteViewModel.note.description}" />
</LinearLayout>
</layout>
这是我的 ViewModel 代码:
public class CreateNoteViewModel extends BaseObservable {
private Note note;
@Bindable
public Note getNote() {
return note;
}
public void setNote(Note note) {
this.note = note;
notifyPropertyChanged(BR.note);
}
}
但是当我尝试 运行 我的应用程序时,我明白了:
Error:Execution failed for task ':app:compileDebugJavaWithJavac'.
java.lang.RuntimeException: Found data binding errors. ****/ data binding error ****msg:Could not find accessor com.justsafe.tmgr.justsafe.screen.createnote.CreateNoteViewModel.note file:C:\android\work\JustSafeTmgr\app\src\main\res\layout\activity_create_note.xml loc:44:33 - 44:62 ****\ data binding error ****
P.S。在我的应用程序的其他地方它可以工作,但我遇到了问题。
尝试将 <import>
标签更改为 <variable>
标签:
<data>
<variable
name="noteViewModel"
type="com.app.screen.createnote.CreateNoteViewModel" />
</data>
build.gradle -- 应用程序
android {
dataBinding {
enabled = true
}
}
dependencies {
def life_versions = "1.1.1"
// Lifecycle components
implementation "android.arch.lifecycle:extensions:$life_versions"
annotationProcessor "android.arch.lifecycle:compiler:$life_versions"
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable
name="LoginViewModel"
type="com.keshav.mvvmdemokeshav.viewModel.LoginViewModel" />
</data>
<!-- TODO Your XML File-->
</layout>