【问题标题】:Kotlin Error When Setting Up Data Binding设置数据绑定时出现 Kotlin 错误
【发布时间】:2020-02-02 05:20:55
【问题描述】:

我正在尝试在我的 MainActivity 中为场景表单片段充气并进行数据竞价。我收到此错误消息:

错误信息

Caused by: android.view.InflateException: Binary XML file line #27: Binary XML file line #27: Error inflating class fragment
 Caused by: android.view.InflateException: Binary XML file line #27: Error inflating class fragment
 Caused by: java.lang.IllegalArgumentException: Binary XML file line #27: Duplicate id 0x7f0800bc, tag null, or parent id 0x7f08007d with another fragment for com.google.ar.sceneform.ux.ArFragment
...
at com.test.test.view.MainActivity.setupDataBinding(MainActivity.kt:54)

这是我的 MainActivity.kt

private fun setupDataBinding() {
    val binding: ActivityMainBinding =
        DataBindingUtil.setContentView(this, R.layout.activity_main)
    binding.lifecycleOwner = this
    binding.viewmodel = viewModel // Injecting the view model into the layout file
}

activity_main.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"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".view.MainActivity">

<data>
    <variable
        name="viewmodel"
        type="com.test.test.viewModel.MainActivityViewModel"/>
</data>

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <FrameLayout
        android:id="@+id/frame_sceneform_fragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <fragment
            android:id="@+id/sceneform_fragment"
            android:name="com.google.ar.sceneform.ux.ArFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </FrameLayout>
    <Button
        android:id="@+id/btnOpenMenu"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="16dp"
        android:onClick="@{() -> viewmodel.openCloseMenu()}"
        android:text="@{viewmodel.btnOpenMenuText}"
        app:layout_constraintEnd_toEndOf="@+id/frame_sceneform_fragment"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>

有没有人见过这个并知道解决方案?

【问题讨论】:

  • 这种错误早于数据绑定。例如,当我在 Google 上搜索 IllegalArgumentException duplicate "with another fragment" 时,我在 SO 上得到了几十个点击,例如 this 和 this。您是否多次夸大此布局?
  • 谢谢!那正是我所做的。在 onCreate 中删除 setContentView 后,它起作用了。

标签: android xml kotlin android-databinding sceneform


【解决方案1】:

如果您使用&lt;fragment&gt;,则您的id 对于整个封闭活动必须是唯一的。这与布局资源中的普通视图不同,在普通视图中,我们可以拥有一组具有相同 id 值的视图(例如,RecyclerView 中的行)。

在您的情况下,您将布局膨胀了两次,因此第二次膨胀导致了第二次 &lt;fragment&gt; 与相同 id 的冲突。

如果您使用数据绑定,DataBindingUtil.setContentView() 会扩大布局并在您的活动上调用 setContentView()。您不需要传统的 setContentView(),在您的情况下,两者都会导致冲突。

【讨论】:

    猜你喜欢
    • 2016-05-21
    • 1970-01-01
    • 1970-01-01
    • 2017-07-22
    • 2021-05-13
    • 1970-01-01
    • 2019-06-04
    • 2021-07-25
    • 1970-01-01
    相关资源
    最近更新 更多