I have an application composed of one activity and several fragments, as recommanded by Google.
- MainActivity
- LobbyFragment
- GameFragment
- ...
I also wanted to use the navigation map
I put a menu in the Activity to have everywhere, and then I would like to switch the content of the page.
To do this as shown in the fragment tutorial, i made the activitymain.xml
to later switch the content of the FragmentContainerView
between fragments.
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:theme="@style/Theme.application.AppBarOverlay">
</com.google.android.material.appbar.AppBarLayout>
<androidx.fragment.app.FragmentContainerView
android:id="@ id/fragment_container_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.my.application.LobbyFragment" /> <!--Here is failure-->
</androidx.coordinatorlayout.widget.CoordinatorLayout>
But, at this point, when I compile, I have :
Unable to start activity ComponentInfo{com.my.application/com.my.application.MainActivity}: android.view.InflateException: Binary XML file line #21: Binary XML file line #21: Error inflating class androidx.fragment.app.FragmentContainerView
And where to put the Activity in the navigation map ?
CodePudding user response:
The documentation link you referenced uses the traditional fragment transaction by directly accessing supportFragmentManager
; but it turns out from your post that you use Navigation Architecture components instead.
In navigation components, you are not supposed to use a particular fragment class in the android:name
property, instead the navController
takes care of that using NavHostFragment
To, fix this, please replace
android:name="com.my.application.LobbyFragment"
With
android:name="androidx.navigation.fragment.NavHostFragment"
Also, you need to reference the name of the navGraph in that with app:navGraph
attribute; assuming it's named as nav_graph
:
app:navGraph="@navigation/nav_graph"