java.lang.IllegalArgumentException: Navigation action/destination (...package and fragments name) cannot be found from the current destination?

Viewed 752

I have a problem like in the title. My app navigate from fragment A to fragment B. And when I want to navigate to fragment C it crash.

There is a function that nav to other fragment

    public void onClick(View v) {
            switch (v.getId()) {
                case R.id.details_start_btn:
                    DetailsFragmentDirections.ActionDetailsFragmentToQuizzFragment action = DetailsFragmentDirections.actionDetailsFragmentToQuizzFragment();
                    action.setQuizId(quizId);
                    action.setTotalQuestions(totalQuestions);
                    navController.navigate(action);
                    break;
                }
            }

And there is my nav.xml

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

    <fragment
        android:id="@+id/startFragment"
        android:name="myapp.quizz.StartFragment"
        android:label="fragment_start"
        tools:layout="@layout/fragment_start" >
        <action
            android:id="@+id/action_startFragment_to_listFragment"
            app:destination="@id/listFragment"
            app:popUpToInclusive="true"
            app:popUpTo="@id/startFragment"
            app:enterAnim="@anim/fade_in"
            app:exitAnim="@anim/fade_out"   />
    </fragment>
    <fragment
        android:id="@+id/listFragment"
        android:name="myapp.quizz.ListFragment"
        android:label="fragment_list"
        tools:layout="@layout/fragment_list" >
        <action
            android:id="@+id/action_listFragment_to_detailsFragment"
            app:destination="@id/detailsFragment"
            app:enterAnim="@anim/slide_in_right"
            app:exitAnim="@anim/slide_out_left"
            app:popEnterAnim="@anim/slide_in_left"
            app:popExitAnim="@anim/slide_out_right"
            />

    </fragment>
    <fragment
        android:id="@+id/detailsFragment"
        android:name="myapp.quizz.DetailsFragment"
        android:label="fragment_details"
        tools:layout="@layout/fragment_details" >
        <action
            android:id="@+id/action_detailsFragment_to_quizzFragment"
            app:destination="@id/quizzFragment"
            app:enterAnim="@anim/slide_up"
            app:exitAnim="@anim/fade_out"
            app:popEnterAnim="@anim/fade_in"
            app:popExitAnim="@anim/slide_down" />
        <argument
            android:name="position"
            app:argType="integer"
            android:defaultValue="0" />
    </fragment>
    <fragment
        android:id="@+id/quizzFragment"
        android:name="myapp.quizz.QuizzFragment"
        android:label="fragment_quizz"
        tools:layout="@layout/fragment_quizz" >
        <action
            android:id="@+id/action_quizzFragment_to_resultFragment"
            app:destination="@id/resultFragment" />
        <argument
            android:name="position"
            app:argType="integer"
            android:defaultValue="0" />
        <argument
            android:name="quizId"
            app:argType="string"
            android:defaultValue="null" />
        <argument
            android:name="totalQuestions"
            app:argType="long"
            android:defaultValue="0L" />
    </fragment>
    <fragment
        android:id="@+id/resultFragment"
        android:name="norbertprosniak.quizz.ResultFragment"
        android:label="fragment_result"
        tools:layout="@layout/fragment_result" />
</navigation>

I found in web that i shoud add this:

    if (it.findNavController().currentDestination?.id == R.id.fragment_B) {
    it.findNavController().navigate(R.id.fragment_A)}

but it make error with "it" nad "currentDestination?.id", and its for Kotlin.

Im trying to fix it from loong time, anyone had problem like this?

P.S. I'm using navigation from jetpack.

0 Answers
Related