即使我,我也会从Android Studio中收到此错误:
这是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 type="android.view.View" alias="v"/>
<variable
name="viewModel"
type="....MyModel"/>
</data>
<android.support.v4.widget.NestedScrollView
android:id="@+id/fragment_question"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical">
<TextView
style="@style/QText.Big"
android:id="@+id/service_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:text="Service name"
android:text="@{viewModel.serviceName}"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_vertical"
android:visibility="@{viewModel.isDateType ? v.VISIBLE : v.GONE}">
<TextView
style="@style/QText.Big"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Date:"/>
<TextView
style="@style/Text.DateTime"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:drawableEnd="@drawable/date_icon"
android:text="@{viewModel.dateAnswer}"
android:onClick="@{viewModel.pickDate}"/>
</LinearLayout>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</layout>
这是我的视图模型:
public class QuestionViewModel extends AbsQuestionViewModel {
public final ObservableString dateAnswer = new ObservableString();
private FragmentQuestionBinding binding;
public QuestionViewModel(Context context, FragmentQuestionBinding binding) {
super(context);
}
public boolean isLabelType() {
return answerType != null && (answerType.equals(ANS_TYPE_LABEL) || answerType.equals(ANS_TYPE_GEOPHOTO));
}
public boolean isDateType() {
return answerType != null && (answerType.equals(ANS_TYPE_DATE) || answerType.equals(ANS_TYPE_DATETIME));
}
public void pickDate(View v) {
DateTime.pickDate(context, dateAnswer::set);
}
}
我不明白我只是因为pickDate
属性而收到错误。我没有得到serviceName
或dateAnswer
属性的错误。
我试图清理和重建项目,并使缓存无效并重新启动。
这是我得到的确切错误:
****/ data binding error ****msg:Could not resolve ...MyModel.pickDate as an accessor or listener on the attribute. file:/.../app/src/main/res/layout/fragment_q.xml loc:90:43 - 90:60 ****\ data binding error ****
答案 0 :(得分:1)
现有代码在使用数据绑定添加xml中的点击侦听器时出错
更改
android:onClick="@{viewModel.pickDate}"
收件人
android:onClick="@{(v)-> viewModel.pickDate(v)}"