[设置数据绑定时出现Kotlin错误

问题描述 投票:1回答:1

我正在尝试在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>

以前有没有人看过并且知道解决方法?

android xml kotlin android-databinding sceneform
1个回答
0
投票

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

在您的情况下,您对布局进行了两次充气,因此第二次充气导致碰撞,而第二个<fragment>id相同。

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

© www.soinside.com 2019 - 2024. All rights reserved.