我试图在我的 MainActivity 中膨胀一个场景 fragment ,并进行数据投标。我收到此错误消息:
错误信息
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>
有没有人见过这个并知道解决方案?
最佳答案
如果您使用 <fragment>
, 你的 id
对于整个封闭 Activity ,必须是唯一的。这与布局资源中的普通 View 不同,在普通 View 中,我们可以拥有一组具有相同 id
的 View 。值(例如 RecyclerView
中的行)。
在您的情况下,您将布局膨胀了两次,因此第二次膨胀导致了第二次 <fragment>
的碰撞。同id
.
如果使用数据绑定(bind),DataBindingUtil.setContentView()
扩展布局并调用 setContentView()
关于你的 Activity 。您不需要传统的 setContentView()
,在你的情况下,两者都导致了碰撞。
关于android - 设置数据绑定(bind)时出现 Kotlin 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60021106/