13

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>
c-an
  • 3,543
  • 5
  • 35
  • 82

7 Answers7

11

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.

ianhanniballake
  • 191,609
  • 30
  • 470
  • 443
  • 11
    I have added android:name="androidx.navigation.fragment.NavHostFragment" and I'm still getting the exact same error – AmirahmadAdibi Nov 07 '19 at 08:18
  • I noticed that even though I have app:navGraph="@navigation/nav_graph" added to my fragment as described in the guide, the property app:navGraph is actually not there. I can write it, but it will never appear in autocomplete - meaning it is useless even if its there... – Chapz Nov 25 '19 at 21:37
  • 14
    @iamhanniballake `` also give's the above error so i am currently avoiding to use fragmentcontainerview.What is solution to that? – Anmol Dec 11 '19 at 18:24
  • 1
    I had this issue in Android Studio 4.1 and 4.2 Previews. Reverted to Android Studio 4.0 Stable and everything works. – Sean Blahovici Jun 11 '20 at 21:33
6

If the accepted answer does not work initially, try "Make Project". Worked for me with <fragment/>

1

If other solutions haven't exactly worked, this worked for me, hopefully helps someone else as well

Especially if you work with Kotlin, check to see whether your dependencies have the same Kotlin versions working (Also anything that has an update or notification or something), then rebuild/sync, takes a bit to load, and at least for me seems to be working fine.

*If didn't work try invalidate catch/restart, who knows

Poorix
  • 106
  • 7
0

if you still get the error after applying what's in the accepted answer then try this - I had the same error, but my activity was working. The error disappeared after I changed the dependencies from android to androidx

nav_version = "2.3.0-alpha03"
implementation "androidx.navigation:navigation-fragment-ktx:$nav_version"
implementation "androidx.navigation:navigation-ui-ktx:$nav_version"
implementation "androidx.navigation:navigation-dynamic-features-fragment:$nav_version"

I also use class rather than android:name in the xml layout

<fragment
    android:id="@+id/nav_host_fragment"
    class="androidx.navigation.fragment.NavHostFragment"
Stachu
  • 1,614
  • 1
  • 5
  • 17
0

Though late hope someone will get benefit. I had the same issue. Used

android:name="androidx.navigation.fragment.NavHostFragment" 
app:navGraph="@navigation/mobile_navigation"

. But was still showing same error. Then finally i solved my issue by looking at build.gradle file.

first my gradle file was like this:

implementation "androidx.navigation:navigation-fragment:2.3.0-beta01"
implementation "androidx.navigation:navigation-ui:2.3.0-beta01" 

then I have added this extra dependency with those dependencies which solved the issue:

implementation "androidx.navigation:navigation-dynamic-features-fragment:2.3.0-beta01"

Best of luck.

Sujon Chondro Shil
  • 105
  • 1
  • 2
  • 10
0

One possible cause for this, at least in my case, was having layout files for portrait and landscape mode, and forgetting to add the androidx.navigation.fragment.NavHostFragment view to both of them. Adding the block to the layout-land/your_activity_layout.xml made the graph to show properly in Android Studio.

Petr Krýže
  • 1,148
  • 1
  • 7
  • 14
-1

Just clean your project Build-> Clean Project

Amr
  • 1,068
  • 12
  • 21