【问题标题】:View must have a tag error in android data bindingView必须有android数据绑定的标签错误
【发布时间】:2019-07-15 03:13:27
【问题描述】:

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

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

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

这样我在活动上设置布局:

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>

【问题讨论】:

    标签: android data-binding


    【解决方案1】:

    您需要在 app_toolbar_layout 布局文件的开头添加布局标签

    【讨论】:

    • 添加 android:tag 并没有解决 app_toolbar_layout 中的问题。
    【解决方案2】:

    当我遇到这个时:

    • 定义布局资源的库模块
    • 依赖于定义相同布局资源的库模块的应用模块
    • 为数据绑定设置了库布局资源(例如,根 &lt;layout&gt; 元素),但该布局资源的应用模块版本没有

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

    【讨论】:

    • 您,先生,是我身穿白色盔甲的闪亮骑士。
    • 我对 28 以上的 API 有第二个布局(layout-v28.xml),它没有包含在 标记中。谢谢!
    【解决方案3】:

    您的所有 XML 视图(纵向、横向、平板电脑等)中必须有 &lt;layout&gt; 标记。即使您必须将&lt;layout&gt; 标签包含在包含的视图中(“@layout/app_toolbar_layout”)。

    【讨论】:

      【解决方案4】:

      保留两个布局,一个带有数据绑定的布局标签,另一个没有它,这是此问题的常见原因。

      当我用相同的名称(/layout 和 /layout-sw720dp)重命名我的两个普通布局文件并使用标签时,我卡住了。然后,它适用于移动设备,但不适用于平板电脑。所以,在清理项目之后,一切都开始工作了。

      【讨论】:

      • 清理项目在我的情况下解决了这个问题。
      • 非常感谢您的解释,非常有用。
      • 确保您在横向和纵向模式下都有布局标签
      【解决方案5】:

      我在使用数组适配器时遇到了这个问题,由于 convertView 上缺少标签而导致崩溃。在我的 getView() 中,我在做:

      override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
          if (convertView == null) {
              DataBindingUtil.inflate<ItemSpinnerDropDownWorkPackageFilterBinding>(
                  LayoutInflater.from(parent.context),
                  R.layout.item_spinner_drop_down_work_package_filter,
                  parent,
                  false
              )
          } else {
              binding = ItemSpinnerDropDownWorkPackageFilterBinding.bind(convertView) 
              binding.text1.setText(getItem(position))
              setDividerVisibility(binding.divider, position)
              return convertView
          }    
      
      }
      

      崩溃了。解决方案是在第一次运行时通过 getView 设置标签:

       override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
          val binding = if (convertView == null) {
              DataBindingUtil.inflate<ItemSpinnerDropDownWorkPackageFilterBinding>(
                  LayoutInflater.from(parent.context),
                  R.layout.item_spinner_drop_down_work_package_filter,
                  parent,
                  false
              )
          } else {
              convertView.tag as ItemSpinnerDropDownWorkPackageFilterBinding
          }
          binding.text1.setText(getItem(position))
          setDividerVisibility(binding.divider, position)
          binding.root.tag = binding
          return binding.root
      
      }
      

      【讨论】:

        【解决方案6】:

        我遇到了类似的错误,所以我的解决方法是

        DataBindingUtil.bind(holder.itemView)?.apply { 项目 = 项目[位置] }

        【讨论】:

          【解决方案7】:

          我只需要在删除资源文件后进行清理和重建。

          【讨论】:

            【解决方案8】:

            可能你有多个 xml 布局文件。

            【讨论】:

            • 请不要发布 cmets 作为答案。或者请提供足够的信息
            【解决方案9】:

            如果您已经在 xml 文件中使用了布局标签,但仍然报错,只需重命名布局文件名并再次清理/重建项目,它将修复

            【讨论】:

              【解决方案10】:

              将 viewStub 与数据绑定一起使用:

              class MyFragment {
                  lateinit var binding: MyFragmentBinding
                  lateinit var viewStubBinding: MyViewStubBinding
                  
                  private fun setViewStub() {
                      binding.myViewStub.setOnInflateListener { viewStub, view ->
                          viewStubBinding = binding.myViewStub.binding as MyViewStubBinding // property viewStubBinding is finally inflated
                      }
                      binding.myViewStub.viewStub?.inflate() // inflate viewStub with defined layout file in XML
                  }
              }
              

              当我尝试使用MyViewStubBinding.bind(view / viewStub) 时,它在View 必须有标签 上崩溃。发生这种情况是因为 ViewStubProxy 尝试自行膨胀绑定类,所以当我尝试自己在同一个 Binding 类上调用 bind() 时,它已经被绑定并因此错误而崩溃。

              【讨论】:

                【解决方案11】:

                我在尝试单独绑定时遇到此错误,因为我的视图在库中膨胀,但我提供了布局。

                文档说你可以这样做:

                val binding: MyLayoutBinding = MyLayoutBinding.bind(viewRoot)
                

                我传递的 viewRoot 是从自定义类派生的,但最终成为我的布局文件(包含布局标记的那个)的父级。所以我不得不通过findViewById将视图指定为我的布局文件的根目录

                【讨论】:

                  猜你喜欢
                  • 1970-01-01
                  • 2021-12-25
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  相关资源
                  最近更新 更多