简体   繁体   English

查看android数据绑定一定有标签错误

[英]View must have a tag error in android data binding

I've two layouts for a screen.我有两个屏幕布局。 Activity works fine while setting a layout for Mobile device but it's causing error while setting layout for tablet device. Activity 在为移动设备设置布局时工作正常,但在为平板设备设置布局时会导致错误。 The main issue is:主要问题是:

Caused by: java.lang.RuntimeException: view must have a tag at com.mypackage.DataBinderMapperImpl.getDataBinder(DataBinderMapperImpl.java:941)原因:java.lang.RuntimeException:视图必须在 com.mypackage.DataBinderMapperImpl.getDataBinder(DataBinderMapperImpl.Z93F725A074133FE1C886F4924)处有标签

Though, I don't face the problem when I install app on mobile device.不过,当我在移动设备上安装应用程序时,我不会遇到这个问题。

This way I'm setting layout on activity:这样我在活动上设置布局:

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

Here is my XML layout for tablet screen:这是我的平板电脑屏幕 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>

I ran into this when I had:我遇到了这个,当我有:

  • A library module defining a layout resource定义布局资源的库模块
  • An app module that depended upon that library module defining the same layout resource依赖于定义相同布局资源的库模块的应用程序模块
  • The library layout resource was set up for data binding (eg, root <layout> element), but the app module's edition of that layout resource was not为数据绑定设置了库布局资源(例如,根<layout>元素),但该布局资源的应用模块版本不是

In my case, the app module's layout was left over from when I created the project.就我而言,应用程序模块的布局是我创建项目时遗留下来的。 Removing it cleared up the problem.删除它解决了问题。

Keeping two layouts, one with layout tag of data-binding and another without it, is the common cause of this issue.保留两个布局,一个带有 data-binding 布局标签,另一个没有它,是这个问题的常见原因。

I got stuck when I renamed my two normal layout files with same name (/layout and /layout-sw720dp) and used tag.当我用相同的名称(/layout 和 /layout-sw720dp)重命名我的两个普通布局文件并使用标签时,我被卡住了。 Then, it worked for mobile device but not for tablet.然后,它适用于移动设备,但不适用于平板电脑。 So, after cleaning project, it all started working.因此,在清理项目后,一切都开始工作了。

I was having this problem when using an array adapter, having a crash due to a missing tag on convertView.我在使用数组适配器时遇到了这个问题,由于 convertView 上缺少标签而导致崩溃。 In my getView(), I was doing:在我的 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
    }    

}

Which was crashing.这是崩溃。 The solution was to set the tag on the first run through getView:解决方案是在第一次运行时通过 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

}

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

You must have <layout> tag into your all XML views (portrait, landscape, tablet, etc.).您必须在所有 XML 视图(纵向、横向、平板电脑等)中使用<layout>标记。 Even you have to include <layout> tag into included views (" @layout/app_toolbar_layout ").即使您必须将<layout>标签包含在包含的视图中(“ @layout/app_toolbar_layout ”)。

i was facing similar error so my work around was我遇到了类似的错误,所以我的解决方法是

DataBindingUtil.bind(holder.itemView)?.apply { item = items[position] } DataBindingUtil.bind(holder.itemView)?.apply { item = items[position] }

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

可能是您有多个 xml 布局文件。

如果您已经在 xml 文件中使用了布局标记,但仍然出现错误,只需重命名布局文件名并再次清理/重建项目即可修复

Using viewStub with databinding:使用带有数据绑定的 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
    }
}

When I tried to use MyViewStubBinding.bind(view / viewStub) it crash on View must have a tag .当我尝试使用MyViewStubBinding.bind(view / viewStub)它崩溃了View must have a tag This happen because ViewStubProxy try to inflate binding class by itself, so when I try to call bind() by myself on same Binding class, it was already bound and crashed with this error.发生这种情况是因为 ViewStubProxy 尝试自行膨胀绑定类,所以当我尝试在同一个 Binding 类上自己​​调用bind() ,它已经被绑定并因此错误而崩溃。

I had this error trying to bind separately as my view is inflated within a library but I supply the layout.我在尝试单独绑定时遇到此错误,因为我的视图在库中膨胀,但我提供了布局。

The doc says you can do this:医生说你可以这样做:

val binding: MyLayoutBinding = MyLayoutBinding.bind(viewRoot)

The viewRoot I was passing was derived from the custom class but ended up being a parent of my layout file (the one that included the layout tag).我传递的 viewRoot 派生自自定义类,但最终成为我的布局文件(包含布局标签的文件)的父级。 So I had to specify the view as the root of my layout file via findViewById所以我必须通过findViewById将视图指定为布局文件的根

除了这个答案,你还需要这个

I encounter this problem, the root case is there are duplicate layout resource file in different module.我遇到这个问题,根本情况是不同模块中有重复的布局资源文件。 delete the redundant one to resolve the issue.删除多余的以解决问题。

In my case worked this code to set content view with data binding.在我的例子中,这段代码使用数据绑定设置内容视图。

binding = GenericItemDetailsListBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 视图必须有标签 - View must have a tag Android数据绑定无法使用View&#39;android:tag&#39;属性 - Android data binding not working with View 'android:tag' property 数据绑定; 视图标签不正确,Android - data binding; view tag isn't correct, Android 我可以在Android上将视图作为数据绑定BindingAdapter的输入吗? - Can I have a View as input to a data binding BindingAdapter on Android? 膨胀类片段时出错。 必须指定唯一的android:id,android:tag,或者具有id的父级 - Error inflating class fragment. Must specify unique android:id, android:tag, or have a parent with an id Android数据绑定 - 错误:(119,29)标识符必须具有XML文件中的用户定义类型。 main_radio_subscribe缺少它 - Android Data binding - Error:(119, 29) Identifiers must have user defined types from the XML file. main_radio_subscribe is missing it 数据绑定未获得“标识符必须具有XML文件中的用户定义类型”错误 - data binding not getting “Identifiers must have user defined types from the XML file” error Android数据绑定错误? - Android data binding error? 数据绑定Android Recycler视图 - Data Binding Android Recycler view Android 数据绑定 - 查看参考 - Android Data Binding - Reference to view
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM