IllegalArgumentException: 当前页面找不到导航动作/目标 packagename/action_xxx。

huangapple go评论79阅读模式
英文:

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

问题

以下是翻译的内容:

我在使用 Android 导航架构组件时遇到了问题,当我尝试在一个 ViewPager 内从一个 Fragment 导航到另一个 Fragment 时,我遇到了以下错误:

java.lang.IllegalArgumentException: 无法从当前目标找到导航动作/目标 
com.gigaweb.mysales:id/action_mainFragment_to_addTransactionFragment。当前目标是 Destination(com.gigaweb.mysales:id/pagerFragment),标签为 fragment_pager,类为 com.gigaweb.mysales.PagerFragment。

我有一个 ViewPager,用于在两个 Fragment 之间进行导航,这部分工作正常。问题在于,我在其中一个 Fragment 中有一个按钮,该按钮也用于使用 navcontroller 导航到另一个 Fragment,当点击按钮时,应用程序崩溃,并出现上述错误。

更新,这是导航图:

正如您所看到的,SignIn Fragment 是起始目标,有一个操作将其链接到 pagerFragment,后者用作 ViewPager 的宿主。

我希望应用程序的工作方式是:

  1. 应用程序启动时显示 SignIn Fragment... 正在工作。

  2. 单击“登录”按钮时导航到 PagerFragment... 正在工作。

  3. 通过向左或向右滑动在 MainFragment 和 AdminFragment 之间进行导航... 正在工作。

  4. 单击 FAB 按钮时导航到 AddTransactionFragment... 不工作!!当单击按钮时,应用程序崩溃。

更新,这是导航图的 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/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>
英文:

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.

IllegalArgumentException: 当前页面找不到导航动作/目标 packagename/action_xxx。

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

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;navigation xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
xmlns:tools=&quot;http://schemas.android.com/tools&quot;
android:id=&quot;@+id/nav_graph&quot;
app:startDestination=&quot;@id/signInFragment&quot;&gt;

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

IllegalArgumentException: 当前页面找不到导航动作/目标 packagename/action_xxx。

答案1

得分: 9

如果您不对MainFragmentAdminFragment进行navigate(),您将保持在您导航到的最后一个目标上:PagerFragment,这是预期的行为。NavController对于诸如PagerFragment中的ViewPager内的子片段一无所知,因此您实际上从未导航到过MainFragment

如果您从未对MainFragmentAdminFragment进行过navigate(),并且它们只能作为ViewPager的一部分可见,则MainFragmentAdminFragment不应该包含在您的导航图中。来自PagerFragment的ViewPager内部片段的任何操作都应直接作用于PagerFragment(实际上是您所在的目标)。

英文:

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).

答案2

得分: 0

针对我的情况,我通过替换以下内容来解决问题 -

<action
    android:id="@+id/action_mainFragment_to_addTransactionFragment"
    app:popUpToInclusive="true" /* 如果为true,则在弹出时也从堆栈中移除目标 */
    app:popUpTo="@id/mainFragment" /* 从目标返回的位置 */
    app:destination="@id/addTransactionFragment" />
英文:

For my case, I resolve the problem by replacing -

 &lt;action
        android:id=&quot;@+id/action_mainFragment_to_addTransactionFragment&quot;
        app:destination=&quot;@id/addTransactionFragment&quot; /&gt;

with

 &lt;action
            android:id=&quot;@+id/action_mainFragment_to_addTransactionFragment&quot;
            app:popUpToInclusive=&quot;true&quot; /* If true then also remove the destination from stack while popup */
            app:popUpTo=&quot;@id/mainFragment&quot;  /*The fragment where to land again from destination*/
            app:destination=&quot;@id/addTransactionFragment&quot; /&gt;

答案3

得分: 0

对于我的情况,事实证明这是因为在导航到新的目标后调用了dismissAllowingStateLoss();引起的。

英文:

For my case, it turns out it caused by calling dismissAllowingStateLoss(); after navigating to new destination.

huangapple
  • 本文由 发表于 2020年9月23日 22:45:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/64030507.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定