【问题标题】:android get Only one layout element and one data element are allowed error when i using presenter for bindingandroid get 只有一个布局元素和一个数据元素在我使用presenter进行绑定时出现错误
【发布时间】:2017-03-14 13:42:23
【问题描述】:

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

只允许一个布局元素和一个数据元素

主布局

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

在这个活动中,我默认没有任何绑定,例如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();
    }
}

【问题讨论】:

    标签: android data-binding android-databinding


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      您的&lt;variable&gt; 元素必须位于&lt;data&gt; 元素内。

      例如:

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

      【讨论】:

        猜你喜欢
        • 2016-07-30
        • 1970-01-01
        • 2018-03-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-06-18
        相关资源
        最近更新 更多