android get Only one layout element and one data element are allowed 报错 when i using presenter for binding

android get Only one layout element and one data element are allowed error when i using presenter for binding

我正在尝试使用没有任何绑定数据的数据绑定到主要布局上的 TextViews 等小部件,但是当我 运行 应用程序时出现此错误:

Only one layout element and one data element are allowed

主布局:

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

    <variable
        name="presenter"
type="com.example.Ui.Register.Presenter.ActivityRegisterPresenter"/>

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#d1d1d1">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/permission_for_read_contacts"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:padding="5dp"
                    android:text="@string/permission_for_read_contacts"
                    android:textColor="@color/white"/>

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="@dimen/default_textview_height"
                    android:text="@string/get_read_contact_permission"
                    android:background="@drawable/selector_blue_buttons"
                    android:clickable="@{()->presenter.getReadContactsPermission()}"
                    android:textColor="@color/white"/>
            </LinearLayout>

            <TextView
                android:id="@+id/activity_register_view_port"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:visibility="gone"/>
        </LinearLayout>
    </FrameLayout>
</layout>

在此 activity 中,我没有任何默认绑定,例如 setText,删除此行后:

<variable
    name="presenter"
    type="com.example.Ui.Register.Presenter.ActivityRegisterPresenter"/>

问题已解决!!

public class ActivityRegisterPresenter {
    private ActivityRegisterContract.View view;

    public ActivityRegisterPresenter(ActivityRegisterContract.View mView) {
        view = mView;
    }

    public void getReadContactsPermission(){
        view.getReadContactsPermission();
    }
}

您的 <variable> 元素需要在 <data> 元素内。

例如:

<data>
    <variable
        name="presenter"
        type="com.example.Ui.Register.Presenter.ActivityRegisterPresenter"/>
</data>

您需要在数据标签中编写变量标签,如下所示:

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

    <variable
        name="presenter"
type="com.example.Ui.Register.Presenter.ActivityRegisterPresenter"/>
    </data>
...

</layout>