Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IllegalArgumentException: Navigation action/destination packagename/action_xxx cannot be found from the current destination

I am having an issue with the Android Navigation Architecture component when I try to navigate from one Fragment to another within a viewpager, I get this error:

java.lang.IllegalArgumentException: Navigation action/destination 
com.gigaweb.mysales:id/action_mainFragment_to_addTransactionFragment cannot be found from the current 
destination Destination(com.gigaweb.mysales:id/pagerFragment) label=fragment_pager class=com.gigaweb.mysales.PagerFragment

I have a viewpager which I use to navigate between two Fragments and it works just fine. the problem is that I have a button within one of the fragments, the button is also used to navigate to another Fragment using navcontroller, and when the button is clicked the app crashes and I get the error above.

Below is the screenshot of the error

Update This is the navGraph

As you can see the SignIn Fragment is the start Destination and there is an action liking it to the pagerFragment which serves as the host for the ViewPager

the way I want the app to work is:

  1. When the app launches The signIn Fragment is shown ..... working

  2. Navigate to PagerFragment when SignIn Button is clicked ..... working

  3. Navigate between MainFragment and AdminFragment by swiping left or right ..... working

  4. Navigate to AddTransactionFragment whin the FAB Button is clicked ..... NOT WORKING!! as the button is clicked the app crashes.

Update Here is the XML code for the Navigation Graph

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

<fragment
    android:id="@+id/signInFragment"
    android:name="com.gigaweb.mysales.SignInFragment"
    android:label="fragment_sign_in"
    tools:layout="@layout/fragment_sign_in" >
    <action
        android:id="@+id/action_signInFragment_to_pagerFragment"
        app:destination="@id/pagerFragment" />
</fragment>
<fragment
    android:id="@+id/addTransactionFragment"
    android:name="com.gigaweb.mysales.AddTransactionFragment"
    android:label="AddTransactionFragment" >
    <action
        android:id="@+id/action_addTransactionFragment_to_mainFragment"
        app:destination="@id/mainFragment" />
</fragment>
<fragment
    android:id="@+id/mainFragment"
    android:name="com.gigaweb.mysales.MainFragment"
    android:label="MainFragment" >
    <action
        android:id="@+id/action_mainFragment_to_addTransactionFragment"
        app:destination="@id/addTransactionFragment" />
</fragment>
<fragment
    android:id="@+id/adminFragment"
    android:name="com.gigaweb.mysales.AdminFragment"
    android:label="AdminFragment" />
<fragment
    android:id="@+id/pagerFragment"
    android:name="com.gigaweb.mysales.PagerFragment"
    android:label="fragment_pager"
    tools:layout="@layout/fragment_pager" />
</navigation>

enter image description here

like image 219
afhamu Avatar asked Dec 31 '22 20:12

afhamu


1 Answers

If you do not navigate() to MainFragment and AdminFragment, you will remain on the last destination you navigated to: PagerFragment, that is the expected behavior. The NavController knows nothing about child fragments such as fragments within a ViewPager of PagerFragment and therefore you were never on MainFragment as a destination.

If you never navigate() to MainFragment or AdminFragment and they are only viewable as part of your ViewPager, then MainFragment and AdminFragment should not be in your graph. Any actions from fragments within PagerFragment's ViewPager should be actions on PagerFragment directly (the destination you're actually on).

like image 85
ianhanniballake Avatar answered Feb 04 '23 17:02

ianhanniballake