当我尝试在视图页面中从一个片段导航到另一个片段时,我遇到了 Android 导航架构组件的问题,出现以下错误:
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,用来在两个片段之间导航,它工作得很好。问题是我在其中一个片段中有一个按钮,该按钮还用于使用导航控制器导航到另一个片段,当单击该按钮时,应用程序崩溃,并且出现上述错误。
Update这是导航图
正如您所看到的,SignIn Fragment 是起始目的地,并且有一个操作喜欢它作为 ViewPager 的主机的 pagerFragment
我希望应用程序的工作方式是:
-
当应用程序启动时,将显示登录片段......正在工作
-
单击登录按钮时导航到 PagerFragment ..... 工作
-
通过向左或向右滑动在 MainFragment 和 AdminFragment 之间导航......正在工作
-
单击 FAB 按钮时导航至“添加事务片段”......不工作!单击按钮时,应用程序崩溃。
Update这是导航图的 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>
如果你不navigate()
to MainFragment
and AdminFragment
,您将保留在上次导航到的目的地:PagerFragment
,这是预期的行为。 NavController 对子片段一无所知,例如 ViewPager 中的片段PagerFragment
因此你从未在MainFragment
作为目的地。
如果你从来没有navigate()
to MainFragment
or AdminFragment
并且它们只能作为 ViewPager 的一部分查看,然后MainFragment
and AdminFragment
不应该be在你的图表中。任何来自片段内的动作PagerFragment
的 ViewPager 应该是以下操作PagerFragment
直接(您实际所在的目的地)。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)