Android导航组件带有底部导航崩溃,如果在弹出后堆栈后导航

发布于 2025-02-09 04:07:02 字数 2160 浏览 2 评论 0原文

我有一个带有附带子图的主导航图,可提供带有底部导航视图的多个背堆。它有效,图形ID与底部菜单项ID一致。当我尝试在重新选择底部项目回调上弹出后堆栈时,出现了主要问题。

例如,三个选项卡:家庭,搜索和帐户。如果重新选择帐户或搜索,则将标签切换到房屋。如果重新选择“主页”选项卡(这是底部导航视图中的第一个)并尝试导航到某些屏幕,则会发生崩溃。它是用于导航到某些屏幕的

java.lang.IllegalArgumentException: Navigation action{spme action} cannot be found from the current destination

,我选择了其他一些选项卡是:

NullPointerException
            at androidx.navigation.ui.NavigationUI.onNavDestinationSelected(NavigationUI.kt:69)

似乎我以某种方式弹出堆栈。 这是main_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"
    android:id="@+id/main_nav_graph"
    app:startDestination="@id/home_graph">
    <include app:graph="@navigation/home_graph" />
    <include app:graph="@navigation/search_graph" />
    <include app:graph="@navigation/account_graph" />
</navigation>

菜单:

    <?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/home_graph"
        android:icon="@drawable/selector_home_nav_bar"
        android:title="@string/label_home" />
    <item
        android:id="@+id/search_graph"
        android:icon="@drawable/selector_search_nav_bar"
        android:title="@string/label_search" />

    <item
        android:id="@+id/action_bookmarks"
        android:icon="@drawable/selector_bookmarks_nav_bar"
        android:title="@string/label_study_tools" />
    <item
        android:id="@+id/account_graph"
        android:icon="@drawable/selector_account_nav_bar"
        android:title="@string/label_account" />
</menu>

带有导航的片段

private fun setNavigation() {
    binding.bottomNavBar.itemIconTintList = null
    binding.bottomNavBar.setOnItemReselectedListener {
        navController?.popBackStack(it.itemId, false)
    }
    navController?.let { NavigationUI.setupWithNavController(binding.bottomNavBar, it) }
}

I have a main nav graph with included subgraphs to provide multiple back stacks with bottom navigation view. It works, graphs ids are consistent with the bottom menu items id. The main problems arise when I try to pop a back stack on the reselect bottom item callback.

For example, three tabs: home, search, and account. If reselect account or search, the tab is being switched to the home. If reselect the home tab (which is the first in the bottom navigation view) and try to navigate to some screen, the crash occurs. It is for navigation to some screens

java.lang.IllegalArgumentException: Navigation action{spme action} cannot be found from the current destination

and and I select some other tab is:

NullPointerException
            at androidx.navigation.ui.NavigationUI.onNavDestinationSelected(NavigationUI.kt:69)

It seems like I pop back stack in some way wrong.
Here is the code of the main_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"
    android:id="@+id/main_nav_graph"
    app:startDestination="@id/home_graph">
    <include app:graph="@navigation/home_graph" />
    <include app:graph="@navigation/search_graph" />
    <include app:graph="@navigation/account_graph" />
</navigation>

The menu:

    <?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/home_graph"
        android:icon="@drawable/selector_home_nav_bar"
        android:title="@string/label_home" />
    <item
        android:id="@+id/search_graph"
        android:icon="@drawable/selector_search_nav_bar"
        android:title="@string/label_search" />

    <item
        android:id="@+id/action_bookmarks"
        android:icon="@drawable/selector_bookmarks_nav_bar"
        android:title="@string/label_study_tools" />
    <item
        android:id="@+id/account_graph"
        android:icon="@drawable/selector_account_nav_bar"
        android:title="@string/label_account" />
</menu>

and the fragment with navigation

private fun setNavigation() {
    binding.bottomNavBar.itemIconTintList = null
    binding.bottomNavBar.setOnItemReselectedListener {
        navController?.popBackStack(it.itemId, false)
    }
    navController?.let { NavigationUI.setupWithNavController(binding.bottomNavBar, it) }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文