I am trying to apply navigation
feature to my project.
And I have this error:
This navigation graph is not referenced to any layout files(expected to find it in at least one layout file with a NavHostFragment with app:navGraph="@navigation/@navigation" attribute
<?xml version="1.0" encoding="utf-8"?>
<navigation 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:id="@+id/navigation"
android:label="fragment_init"
app:startDestination="@id/initFragment"
>
<fragment
android:id="@+id/initFragment"
android:label="fragment_init"
tools:layout="@layout/fragment_init">
<action
android:id="@+id/action_initFragment_to_authenticationFragment5"
app:destination="@id/authenticationFragment"
/>
<action
android:id="@+id/action_initFragment_to_settingFragment3"
app:destination="@id/settingFragment" />
</fragment>
<fragment
android:id="@+id/authenticationFragment"
android:name="com.example.AuthenticationFragment"
android:label="fragment_authentication"
tools:layout="@layout/fragment_authentication" />
<fragment
android:id="@+id/settingFragment"
android:name="com.example.view.main.fragment.SettingFragment"
android:label="SettingFragment"
tools:layout="@layout/fragment_setting" />
</navigation>
I added that attribute here and there (navigation and fragments ). And also, the layout files in layout folder which are used in navigation.xml. But didn't work.
This is activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
android:background="#000"
android:tint="#555"
tools:context=come.example.view.main.MainActivity">
<ImageView
android:id="@+id/iv_flame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true" />
<fragment
android:id="@+id/nav_host_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:navGraph="@navigation/navigation"/>
</RelativeLayout>
If the accepted answer does not work initially, try "Make Project". Worked for me with <fragment/>
You haven't set up for your <fragment>
correctly - every <fragment>
needs an android:name
pointing to the Fragment class it is loading. In the case of Navigation, it must reference the androidx.navigation.fragment.NavHostFragment
as per the Getting Start documentation:
<fragment
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:navGraph="@navigation/navigation"/>
Once you actually tie your navigation graph to a NavHostFragment
, the error will go away.
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