Giacomo M
Giacomo M

Reputation: 4723

data binding error ****msg:Could not resolve as an accessor or listener on the attribute

I get this error from Android Studio even if I:

This is the 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>

This is my view model:

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);
    }
}

What I dont understand is that I get the error just for pickDate attribute. I do not get the error for example for serviceName or dateAnswer attributes.

I tried to clean and rebuild the project and to invalidates cache and restart.

This is the exact error I get:

****/ 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 ****

Upvotes: 3

Views: 2058

Answers (1)

mahdi shahbazi
mahdi shahbazi

Reputation: 2152

The existing code has error on adding on click listener in xml using databinding

change

android:onClick="@{viewModel.pickDate}"

To

android:onClick="@{(v)-> viewModel.pickDate(v)}"

Upvotes: 4

Related Questions