Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

exception: View does not have a navController set

in the fragment which i made it host in the tag fragment in activity when i use navController = Navigation.findNAvController(view) the app crashes by the error: View does not have a navController set.

this is nav_graph:

<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/nav_graph"
    app:startDestination="@id/mainFragment">

    <fragment
        android:id="@+id/mainFragment"
        android:name="studio.apptick.mouj.view.fragments.MainFragment"
        tools:layout="@layout/fragment_main"
        android:label="fragment_main" >
        <action
            android:id="@+id/action_mainFragment_to_playlistActivityFragment"
            app:destination="@id/playlistActivityFragment" />
        <action
            android:id="@+id/action_mainFragment_to_searchActivity"
            app:destination="@id/searchActivity" />
    </fragment>
    <activity
        android:id="@+id/searchActivity"
        android:name="studio.apptick.mouj.view.activity.SearchActivity"
        android:label="activity_search"
        tools:layout="@layout/activity_search" />
</navigation>

this is fragment tag in activity:

        android:id="@+id/nav_host_fragment"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toTopOf="@id/view_player"
        android:name="androidx.navigation.fragment.NavHostFragment"
        app:defaultNavHost="true"
        app:navGraph="@navigation/nav_graph">
    </fragment>
like image 285
james rm Avatar asked Aug 27 '19 21:08

james rm


3 Answers

I got today alike error

ava.lang.IllegalStateException: Activity com.example.roomtutorial.MainActivity@2a8d7bb does not have a NavController set on 2131231116

I get this error pointing on setupActionBarWithNavController(findNavController(R.id.mainFragment))

So the problem was in XML

<androidx.fragment.app.FragmentContainerView
    android:id="@+id/fragment"
    android:name="androidx.navigation.fragment.NavHostFragment"

etc..... I delt with a problem by changing <androidx.fragment.app.FragmentContainerView to <fragment

Someone who designs the option really messed it up.

like image 103
Valery Lavrov Avatar answered Oct 20 '22 00:10

Valery Lavrov


If you want navController in activity you can find it via

navController = Navigation.findNavController(Activity, R.id.nav_host_fragment)

or in a fragment

navController = NavHostFragment.findNavController(Fragment)

or

navController = Navigation.findNavController(View)
like image 21
asad mahmood Avatar answered Oct 19 '22 23:10

asad mahmood


You can use the Kotlin Extension Function to navigate to the destination.

findNavController().navigate(R.id.action_homeFragment_to_destinationFragment)

Also, make sure you use to replace the fragment tag with FragmentContainerView as recommended by android.

<androidx.fragment.app.FragmentContainerView
   android:id="@+id/my_nav_host_fragment"
   android:layout_width="match_parent"
   android:name="androidx.navigation.fragment.NavHostFragment"
   app:defaultNavHost="true"
   app:navGraph="@navigation/nav_graph"
   android:layout_height="match_parent"/>
like image 43
Chintan Parmar Avatar answered Oct 20 '22 00:10

Chintan Parmar