gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 00:54:09 24 4
gpt4 key购买 nike

当我尝试在 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 之间导航,它工作得很好。问题是我在其中一个 fragment 中有一个按钮,该按钮还用于使用 navcontroller 导航到另一个 fragment ,当单击该按钮时,应用程序崩溃并出现上述错误。
Below is the screenshot of the error
更新 这是导航图
如您所见,SignIn Fragment 是起始目标,并且有一个喜欢它的 Action 到 pagerFragment,它充当 ViewPager 的主机
我希望应用程序工作的方式是:
  • 当应用程序启动时显示登录 fragment .....工作
  • 单击登录按钮时导航到 PagerFragment .....工作
  • 通过向左或向右滑动来在 MainFragment 和 AdminFragment 之间导航.....工作
  • 单击 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>
    enter image description here

    最佳答案

    如果你不navigate()MainFragmentAdminFragment ,您将停留在您导航到的最后一个目的地:PagerFragment ,这是预期的行为。 NavController 对子 fragment (例如 PagerFragment 的 ViewPager 中的 fragment )一无所知。因此你从来没有上过MainFragment作为目的地。
    如果你从来没有navigate()MainFragmentAdminFragment它们只能作为 ViewPager 的一部分查看,然后是 MainFragmentAdminFragment不应该 在你的图表中。来自 PagerFragment 中的 fragment 的任何操作的 ViewPager 应该是对 PagerFragment 的操作直接(您实际所在的目的地)。

    关于java - IllegalArgumentException : Navigation action/destination packagename/action_xxx cannot be found from the current destination,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64030507/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com