在 Android Studio 的数据绑定库中找不到 setHandler 的符号方法
Can not Find Symbol Method for setHandler in Data Binding Library in Android Studio
我在 android studio 中使用数据绑定库,我创建了处理程序来检测 onlcik()
事件我正在使用以下代码
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){
}
}
}
当我尝试构建项目时出现错误
Error:(134, 29) error: cannot find symbol method setHandler(MainActivity.MyHandlers)
搜索后我发现 bind:handlers="@{handlers}"
中的名称必须与 data
标记中包含的视图中使用的名称相同,清理并重建项目并且它工作
我在 android studio 中使用数据绑定库,我创建了处理程序来检测 onlcik()
事件我正在使用以下代码
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){
}
}
}
当我尝试构建项目时出现错误
Error:(134, 29) error: cannot find symbol method setHandler(MainActivity.MyHandlers)
搜索后我发现 bind:handlers="@{handlers}"
中的名称必须与 data
标记中包含的视图中使用的名称相同,清理并重建项目并且它工作