[英]View must have a tag error in android data binding
我有兩個屏幕布局。 Activity 在為移動設備設置布局時工作正常,但在為平板設備設置布局時會導致錯誤。 主要問題是:
原因:java.lang.RuntimeException:視圖必須在 com.mypackage.DataBinderMapperImpl.getDataBinder(DataBinderMapperImpl.Z93F725A074133FE1C886F4924)處有標簽
不過,當我在移動設備上安裝應用程序時,我不會遇到這個問題。
這樣我在活動上設置布局:
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>
我遇到了這個,當我有:
<layout>
元素),但該布局資源的應用模塊版本不是就我而言,應用程序模塊的布局是我創建項目時遺留下來的。 刪除它解決了問題。
保留兩個布局,一個帶有 data-binding 布局標簽,另一個沒有它,是這個問題的常見原因。
當我用相同的名稱(/layout 和 /layout-sw720dp)重命名我的兩個普通布局文件並使用標簽時,我被卡住了。 然后,它適用於移動設備,但不適用於平板電腦。 因此,在清理項目后,一切都開始工作了。
我在使用數組適配器時遇到了這個問題,由於 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
}
您需要在 app_toolbar_layout 布局文件的開頭添加布局標簽
您必須在所有 XML 視圖(縱向、橫向、平板電腦等)中使用<layout>
標記。 即使您必須將<layout>
標簽包含在包含的視圖中(“ @layout/app_toolbar_layout
”)。
我遇到了類似的錯誤,所以我的解決方法是
DataBindingUtil.bind(holder.itemView)?.apply { item = items[position] }
我只需要在刪除資源文件后清理和重建。
可能是您有多個 xml 布局文件。
如果您已經在 xml 文件中使用了布局標記,但仍然出現錯誤,只需重命名布局文件名並再次清理/重建項目即可修復
使用帶有數據綁定的 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 must have a tag 。 發生這種情況是因為 ViewStubProxy 嘗試自行膨脹綁定類,所以當我嘗試在同一個 Binding 類上自己調用bind()
,它已經被綁定並因此錯誤而崩潰。
我在嘗試單獨綁定時遇到此錯誤,因為我的視圖在庫中膨脹,但我提供了布局。
醫生說你可以這樣做:
val binding: MyLayoutBinding = MyLayoutBinding.bind(viewRoot)
我傳遞的 viewRoot 派生自自定義類,但最終成為我的布局文件(包含布局標簽的文件)的父級。 所以我必須通過findViewById
將視圖指定為布局文件的根
我遇到這個問題,根本情況是不同模塊中有重復的布局資源文件。 刪除多余的以解決問題。
在我的例子中,這段代碼使用數據綁定設置內容視圖。
binding = GenericItemDetailsListBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.