英文:
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 的宿主。
我希望应用程序的工作方式是:
-
应用程序启动时显示 SignIn 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>
英文:
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.
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:
-
When the app launches The signIn Fragment is shown ..... working
-
Navigate to PagerFragment when SignIn Button is clicked ..... working
-
Navigate between MainFragment and AdminFragment by swiping left or right ..... working
-
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>
答案1
得分: 9
如果您不对MainFragment
和AdminFragment
进行navigate()
,您将保持在您导航到的最后一个目标上:PagerFragment
,这是预期的行为。NavController对于诸如PagerFragment
中的ViewPager内的子片段一无所知,因此您实际上从未导航到过MainFragment
。
如果您从未对MainFragment
或AdminFragment
进行过navigate()
,并且它们只能作为ViewPager的一部分可见,则MainFragment
和AdminFragment
不应该包含在您的导航图中。来自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 -
<action
android:id="@+id/action_mainFragment_to_addTransactionFragment"
app:destination="@id/addTransactionFragment" />
with
<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" />
答案3
得分: 0
对于我的情况,事实证明这是因为在导航到新的目标后调用了dismissAllowingStateLoss();
引起的。
英文:
For my case, it turns out it caused by calling dismissAllowingStateLoss();
after navigating to new destination.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论