Rajat Naik
Rajat Naik

Reputation: 41

Android Navigation java.language.IllegalStateException fragment class was not set

Navigation Drawer

In this when I click Saved Result it shows all the saved results with date. The image showing saved results in SavedResultsFragment
Navigation Layout

When I click on any item it should show all the details of that clicked item in ShowSavedResltsFragment but instate app crashes and IllegalStateException is raised with fragment class was not set.

The image showing navigation layout and code
The image showing code logic where trying to switch to ShowResultsFragment from SavedResultFragment

I'm using safe args and all but only issue is faced when I try to navigate between SavedResultFragment to ShowSavedResultsFragment. I tried to search official documentation and Stack Overflow but didn't found anything which I was facing. Official documentation gives a hint that navHostfragment is not implemented properly then this can trigger above exception but I'm using their official documentation but only thing I have done here is navigating to a Fragment through Navigation Drawer and then from that fragment trying to navigate to another fragment and this is where it breaks.

Image showing exception Edit: NavHostFragment code

<?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">

    <data>

    </data>

    <merge>
        <androidx.drawerlayout.widget.DrawerLayout
            android:id="@+id/drawerLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <androidx.constraintlayout.widget.ConstraintLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                tools:context=".MainActivity">

                <androidx.fragment.app.FragmentContainerView
                    android:id="@+id/myNavHostFragment"
                    android:name="androidx.navigation.fragment.NavHostFragment"
                    android:layout_width="0dp"
                    android:layout_height="0dp"
                    app:defaultNavHost="true"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintLeft_toLeftOf="parent"
                    app:layout_constraintRight_toRightOf="parent"
                    app:layout_constraintTop_toTopOf="parent"
                    app:navGraph="@navigation/navigation" />

            </androidx.constraintlayout.widget.ConstraintLayout>

            <com.google.android.material.navigation.NavigationView
                android:id="@+id/navVew"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_gravity="start"
                app:headerLayout="@layout/nav_header"
                app:menu="@menu/nav_drawer_menu" />
        </androidx.drawerlayout.widget.DrawerLayout>
    </merge>
</layout>

Upvotes: 4

Views: 1811

Answers (3)

Francis S
Francis S

Reputation: 174

The declaration of your ShowSavedResultsFragment inside your nav graph doesn't have a name attribute attached to it.

If you look at the SavedResultFragment declaration, you'll notice it has a name attribute.

Add a name attribute to your ShowSavedResultsFragment declaration. The value of the name attribute should be the package name of the ShowSavedResultsFragment class .(dot) the name of the class. Something like com.xxx.yyy.savedresult.ShowSavedResultsFragment

Upvotes: 4

Rajat Naik
Rajat Naik

Reputation: 41

There was a bug in ShowSavedResultsFragment due to which the exception was raised. Still I was not able to figure out where that bug was so I deleted the fragment and associated view models and re created them and the exception was not raised again and everything worked fine.
Edit: There was not a real bug introduced by me but I had changed class name and this type of things can raise exception because under the hood IDE does mapping and generating class names on it's own and linking them so Invalidating Caches and rebuilding the project would had helped.

Upvotes: 0

Varsha Kulkarni
Varsha Kulkarni

Reputation: 1564

This is what is given in Fragment's findNavController() documentation- "Calling this on a Fragment that is not a [NavHostFragment] or within a [NavHostFragment] will result in an [IllegalStateException]". Just replace it with

 Navigation.findNavController(requireActivity(), R.id.myNavHostFragment)
                        .navigate(..)

Upvotes: 0

Related Questions