【问题标题】:IllegalArgumentException: Navigation action/destination packagename/action_xxx cannot be found from the current destinationIllegalArgumentException:无法从当前目的地找到导航操作/目的地包名/action_xxx
【发布时间】:2021-01-09 19:19:54
【问题描述】:

当我尝试在 viewpager 中从一个 Fragment 导航到另一个 Fragment 时,我遇到了 Android Navigation Architecture 组件的问题,我收到此错误:

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

我有一个 viewpager,用于在两个 Fragment 之间导航,它工作得很好。问题是我在其中一个片段中有一个按钮,该按钮还用于使用 navcontroller 导航到另一个片段,当单击该按钮时,应用程序崩溃并出现上述错误。

更新这是导航图

如您所见,SignIn Fragment 是起始目的地,并且有一个动作喜欢它作为 ViewPager 的主机的 pagerFragment

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

  1. 当应用程序启动时,会显示登录片段.....工作

  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>

【问题讨论】:

  • 那么在&lt;fragment android:id="@+id/pagerFragment" 目标中是否有一个名为action_mainFragment_to_addTransactionFragment&lt;action&gt; 元素?因为当onClick 触发时,这就是您所在的目的地。请显示您的导航图以及这些目的地与您的 ViewPager 的关系。
  • @ianhanniballake 是的,您提到的上述动作是按钮单击触发的动作。我在更新中添加了导航图的屏幕截图
  • 请在您的问题中包含实际的导航 XML 文件,而不仅仅是设计图面的屏幕截图。
  • @ianhanniballake 好的

标签: java android navigationcontroller


【解决方案1】:

如果您没有 navigate()MainFragmentAdminFragment,您将停留在您导航到的最后一个目的地:PagerFragment,这是预期的行为。 NavController 对子片段(例如 PagerFragment 的 ViewPager 中的片段)一无所知,因此您从未将 MainFragment 作为目的地。

如果您从未从navigate()MainFragmentAdminFragment 并且它们只能作为您的ViewPager 的一部分查看,那么MainFragmentAdminFragment 不应在您的图表中。来自 PagerFragment 的 ViewPager 中的片段的任何操作都应该是直接在 PagerFragment 上的操作(您实际所在的目的地)。

【讨论】:

  • 我明白了。但是有什么方法可以在 ViewPager 的子片段之间导航?
  • ViewPager 当然可以让你调用setCurrentItem(indexOfThePageYouWantToGoTo),但这与导航组件无关。这与导航到您的 AddTransactionFragment 有什么关系?
  • 问题是我希望导航发生在按钮单击并且 AddTransactionFragment 不是 ViewPager 的子项
  • 从 ViewPager 的任何子片段导航到 AddTransactionFragment 是完全可以的,只要您触发的操作位于您的 PagerFragment - 您实际所在的目的地。
【解决方案2】:

在我的情况下,它是由导航到新目的地后调用dismissAllowingStateLoss(); 引起的。

【讨论】:

    【解决方案3】:

    就我而言,我通过替换来解决问题 -

     <action
            android:id="@+id/action_mainFragment_to_addTransactionFragment"
            app:destination="@id/addTransactionFragment" />
    

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

    【讨论】:

      猜你喜欢
      • 2021-12-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-27
      • 1970-01-01
      • 2021-10-15
      相关资源
      最近更新 更多