视图在Android数据绑定中必须存在标签错误

Rah*_*ogi 3 data-binding android

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

造成原因:java.lang.RuntimeException:视图必须在com.mypackage.DataBinderMapperImpl.getDataBinder(DataBinderMapperImpl.java:941)处具有标记

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

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

val resetPasswordActivityBinding = DataBindingUtil.setContentView<ResetPasswordActivityBinding>(this,
                R.layout.reset_password_activity)
resetPasswordActivityBinding.resetPasswordViewModel = resetPasswordViewModel
Run Code Online (Sandbox Code Playgroud)

这是我的平板电脑屏幕的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>
Run Code Online (Sandbox Code Playgroud)

Rah*_*ogi 17

保留两个布局,一个带有 data-binding 布局标签,另一个没有它,是这个问题的常见原因。

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

  • 清理项目解决了我的问题。 (3认同)

Kri*_*lsh 8

我在使用数组适配器时遇到了这个问题,由于 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
    }    

}
Run Code Online (Sandbox Code Playgroud)

这是崩溃的。解决方案是在第一次运行时通过 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

}
Run Code Online (Sandbox Code Playgroud)


Com*_*are 7

我有以下情况时遇到了这个问题:

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

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

  • 先生,您是我身披白甲的闪亮骑士。 (2认同)

mtr*_*kal 5

将 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
    }
}
Run Code Online (Sandbox Code Playgroud)

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


小智 3

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