1

I am trying to inflate a sceneform fragment in my MainActivity and also do data bidning. I get this error message:

Error message

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)

This is my 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>

Has anyone seen this before and know a solution?

Casper Lindberg
  • 1,053
  • 1
  • 11
  • 16
  • 1
    This sort of error well predates data binding. When I search Google for `IllegalArgumentException duplicate "with another fragment"`, for example, I get a couple of dozen hits just here on SO, such as [this](https://stackoverflow.com/q/14083950/115145) and [this](https://stackoverflow.com/q/15562416/115145). Are you inflating this layout multiple times perhaps? – CommonsWare Feb 01 '20 at 21:14
  • Thanks! That was exactly what I did. After removing setContentView in onCreate, it worked. – Casper Lindberg Feb 01 '20 at 21:15

1 Answers1

1

If you use <fragment>, your id must be unique for the whole enclosing activity. That is different from a normal view in a layout resource, where we can have a bunch of them with the same id value (e.g., rows in a RecyclerView).

In your case, you were inflating the layout twice, and so the second inflation caused a collision where you had a second <fragment> with the same id.

If you use data binding, DataBindingUtil.setContentView() inflates the layout and calls setContentView() on your activity. You do not need a traditional setContentView(), and in your case, having both resulted in the collision.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491