Search code examples
androidandroid-studioandroid-studio-2.2android-databinding

Can not Find Symbol Method for setHandler in Data Binding Library in Android Studio


I am using data binding library in android studio and I created handler to detect the onlcik() events I am using the following code

main_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<layout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <data>
        <variable
            name="handlers"
            type="com.example.MainActivity.MyHandlers"/>
    </data>
        <RelativeLayout
            android:orientation="vertical"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:layout_width="wrap_content"
            android:layout_height="match_parent">


                <include
                        android:layout_height="wrap_content"
                        layout="@layout/grid_layout_carinfo"
                        app:includedViewGroup="@{1}"
                        bind:handlers="@{handlers}"
                        android:layout_width="wrap_content"/>

    </RelativeLayout>
</layout>

grid_layout_carinfo.xml

<layout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">
    <data>

        <variable
            name="handlers"
            type="com.example.MainActivity.MyHandlers"/>
        <variable
            name="includedViewGroup"
            type="int"/>
    </data>

    <GridLayout
        android:id="@+id/GridLayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:columnCount="3"
        android:rowCount="4"
        android:orientation="horizontal">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_gravity="center_vertical"
            android:layout_marginLeft="@dimen/driver_info_view_margin"
            android:layout_marginRight="@dimen/driver_info_view_margin"
            android:layout_column="0"
            android:layout_row="0">

            <ImageView
                    android:id="@+id/car_photo_front_view"
                    android:src="@mipmap/abc"
                    android:layout_marginTop="8dp"
                    android:onClick="@{(view)->handlers.onCarPhotoFrontView(view)}"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    tools:ignore="ContentDescription" />


        </LinearLayout>
    </GridLayout>
</layout>

MainActivity.java

public class MainActivity extends AppCompatActivity {
 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MainActivityBinding binding = DataBindingUtil.setContentView(this, R.layout.main_activity);


}

    public class MyHandlers {
        public void onClickItem(View view){

        }
    }

}

when i try to build the project i get the error

Error:(134, 29) error: cannot find symbol method setHandler(MainActivity.MyHandlers)

Solution

  • After Searching I find out the answer that the name in bind:handlers="@{handlers}" must be same which is use in the included view in data tag , clean and rebuild the project and It work