Issue I am trying to add a fragment in activity, along with Navigation. But I see the following error
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.app.myapp/com.app.myapp.HomeMainActivity}: android.view.InflateException: Binary XML file line #12: Binary XML file line #12: Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2927)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2988)
at android.app.ActivityThread.-wrap14(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1631)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6682)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
Caused by: android.view.InflateException: Binary XML file line #12: Binary XML file line #12: Error inflating class fragment
Caused by: android.view.InflateException: Binary XML file line #12: Error inflating class fragment
Caused by: java.lang.IllegalArgumentException: Binary XML file line #12: Duplicate id 0x7f0900ed, tag null, or parent id 0xffffffff with another fragment for androidx.navigation.fragment.NavHostFragment
Code
activity_home_new.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">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorPrimaryDark"
tools:context=".HomeMainActivity">
<fragment
android:id="@+id/my_nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="0dp"
android:layout_height="0dp"
app:defaultNavHost="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="@navigation/nav_graph" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
No changes made in Fragment and Activity class.
Solution which I tried
I tried changing fragment to framelayout and FragmentContainerView in ctivity_home_new.xml The app doesn't; crash.
My opinion about the crash
The crash is something related to with "androidx.navigation.fragment.NavHostFragment". There are few questions something similar that is related to MapFragment.
Not looking for the code, I just want to know the root cause for the crash
It seems that NavHostFragment is just a coincidental passwalker. This problem is related to inflating XML content twice.
In my case what I was doing:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivitySigninBinding.inflate(layoutInflater)
setContentView(R.layout.activity_signin)
}
and it was working until I split my content into more fragments. The fix was:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivitySigninBinding.inflate(layoutInflater)
setContentView(binding.root) // Notice the binding root
}
Hope it helps, spend some time to figure it out.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With