Android 设置数据绑定时出现Kotlin错误
我试图在我的主要活动中膨胀一个场景表单片段,并进行数据绑定。我收到以下错误消息: 错误消息Android 设置数据绑定时出现Kotlin错误,android,xml,kotlin,android-databinding,sceneform,Android,Xml,Kotlin,Android Databinding,Sceneform,我试图在我的主要活动中膨胀一个场景表单片段,并进行数据绑定。我收到以下错误消息: 错误消息 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 infl
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)
这是我的主要活动
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>
以前有人见过这种情况并知道解决方案吗?如果您使用
,您的id对于整个封闭活动必须是唯一的。这与布局资源中的普通视图不同,在布局资源中,我们可以拥有一组具有相同id
值的视图(例如,RecyclerView
中的行)
在您的例子中,您两次对布局进行了充气,因此第二次充气导致了一次碰撞,您的第二次
具有相同的id
如果使用数据绑定,DataBindingUtil.setContentView()
会扩大布局并对活动调用setContentView()
。您不需要传统的setContentView()
,在您的情况下,两者都会导致冲突。这种错误早于数据绑定。例如,当我在Google上搜索带有另一个片段的“IllegalArgumentException duplicate”
时,我在这里得到了几十个点击,比如和。你是不是把这个布局放大了很多次?谢谢!那正是我所做的。在onCreate中删除setContentView后,它工作了。