android - view must have a tag error in android data binding

标签 android data-binding

我有两种屏幕布局。 Activity 在为移动设备设置布局时工作正常,但在为平板设备设置布局时导致错误。主要问题是:

Caused by: java.lang.RuntimeException: view must have a tag at com.mypackage.DataBinderMapperImpl.getDataBinder(DataBinderMapperImpl.java:941)

不过,当我在移动设备上安装应用程序时,我没有遇到这个问题。

我通过这种方式设置 Activity 布局:

val resetPasswordActivityBinding = DataBindingUtil.setContentView<ResetPasswordActivityBinding>(this,
                R.layout.reset_password_activity)
resetPasswordActivityBinding.resetPasswordViewModel = resetPasswordViewModel

这是我的平板电脑屏幕 XML 布局:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <data>

        <variable
            name="resetPasswordViewModel"
            type="com.bhi.salesarchitect.user.password.reset.ResetPasswordViewModel" />

    </data>

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

        <include
            android:id="@+id/toolbar"
            layout="@layout/app_toolbar_layout"
            app:appTheme="@{resetPasswordViewModel.appTheme}"
            app:appToolbar="@{resetPasswordViewModel.appToolbar}" />

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@drawable/bg_splash"
                android:contentDescription="@null"
                android:scaleType="centerCrop" />

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

                <ImageView
                    android:id="@+id/imv_builder_logo_change_pswd"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="@dimen/dp_135"
                    android:layout_marginBottom="@dimen/space_xxlarge"
                    android:layout_weight=".5"
                    android:contentDescription="@null"
                    android:src="@drawable/ic_logo" />

                <FrameLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_marginEnd="@dimen/dp_80"
                    android:layout_weight=".4">

                    <ImageView
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_marginStart="@dimen/space_normal"
                        android:layout_marginEnd="@dimen/space_normal"
                        android:contentDescription="@null"
                        android:scaleType="fitXY"
                        android:src="@drawable/bg_white_shadow" />

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:orientation="vertical"
                        android:paddingStart="@dimen/space_xxxlarge"
                        android:paddingEnd="@dimen/space_xxxlarge">

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginTop="@dimen/dp_55"
                            android:layout_marginBottom="@dimen/space_small"
                            android:gravity="center"
                            android:text="@string/a_one_time_password_reset_code_has_been_sent_to_your_email"
                            android:textColor="@color/blue_dark_main"
                            android:textSize="@dimen/text_size_normal" />

                        <EditText
                            android:id="@+id/et_otp"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_marginTop="@dimen/space_normal"
                            android:background="@drawable/shape_rounded_white"
                            android:drawablePadding="@dimen/space_small"
                            android:hint="@string/password_reset_code"
                            android:inputType="textPersonName"
                            android:paddingStart="@dimen/space_small"
                            android:paddingTop="@dimen/space_xsmall"
                            android:paddingEnd="@dimen/space_xxsmall"
                            android:paddingBottom="@dimen/space_xsmall"
                            android:textColor="@android:color/black"
                            android:textSize="@dimen/sp_15" />

                        <EditText
                            android:id="@+id/et_password"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_marginTop="@dimen/space_small"
                            android:background="@drawable/shape_rounded_white"
                            android:drawableStart="@drawable/ic_password"
                            android:drawablePadding="@dimen/space_small"
                            android:hint="@string/new_password"
                            android:inputType="textPassword"
                            android:maxLength="@integer/max_password_length"
                            android:paddingStart="@dimen/space_small"
                            android:paddingTop="@dimen/space_xsmall"
                            android:paddingEnd="@dimen/space_xxsmall"
                            android:paddingBottom="@dimen/space_xsmall"
                            android:textColor="@android:color/black"
                            android:textSize="@dimen/sp_15" />

                        <EditText
                            android:id="@+id/et_confirm_password"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_marginTop="@dimen/space_small"
                            android:background="@drawable/shape_rounded_white"
                            android:drawableStart="@drawable/ic_password"
                            android:drawablePadding="@dimen/space_small"
                            android:hint="@string/confirm_new_password"
                            android:imeOptions="actionDone"
                            android:inputType="textPassword"
                            android:maxLength="@integer/max_password_length"
                            android:paddingStart="@dimen/space_small"
                            android:paddingTop="@dimen/space_xsmall"
                            android:paddingEnd="@dimen/space_xxsmall"
                            android:paddingBottom="@dimen/space_xsmall"
                            android:textColor="@android:color/black"
                            android:textSize="@dimen/sp_15" />

                        <Button
                            android:id="@+id/bt_submit"
                            style="@style/ButtonNormal"
                            android:layout_marginTop="@dimen/space_normal"
                            android:backgroundTint="@color/blue_dark_main"
                            android:onClick="@{()-> resetPasswordViewModel.onSubmitClick()}"
                            android:text="@string/submit"
                            android:textColor="@android:color/white" />

                    </LinearLayout>

                </FrameLayout>

            </LinearLayout>

        </FrameLayout>
    </LinearLayout>
</layout>

最佳答案

当我遇到这个问题时:

  • 定义布局资源的库模块
  • 依赖于定义相同布局资源的库模块的应用程序模块
  • 库布局资源是为数据绑定(bind)设置的(例如,根 <layout> 元素),但该布局资源的应用模块版本不是

在我的例子中,应用程序模块的布局是我创建项目时遗留下来的。删除它可以解决问题。

关于android - view must have a tag error in android data binding,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54803137/

相关文章:

android - Android 中的 iOS 选项卡替换

c# - 在 'code behind' 中设置的 WPF 控件属性,之后忽略绑定(bind)

java - 为什么旋转会导致Android fragment 替换失败?

安卓和 ICU 5.0

java - TabHost/TabWidget View 未在父级中居中

c# - 将 DataGrid 列宽绑定(bind)到另一个 DataGrid 的两列

c# - 将 WPF DataGrid 控件数据绑定(bind)到 System.Data.DataTable 对象?

c# - 数据绑定(bind)主窗口的标题以查看模型的属性

java - 在 Java 中删除 json 中的节点

android - hierarchyviewer 可以与 Nexus 以外的真实设备一起使用吗?