问题
I googled but still didn't find solution for me.
This is my 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>
This is my ViewModel code:
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);
}
}
But when I try run my app I got it:
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. In other place in my app it work, but there I get problem.
回答1:
Try to change the <import> tag to a <variable> tag:
<data>
<variable
name="noteViewModel"
type="com.app.screen.createnote.CreateNoteViewModel" />
</data>
回答2:
build.gradle -- app
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>
来源:https://stackoverflow.com/questions/43351048/databinding-error-could-not-find-accessor