BottomAppBar + NavigationView + FloatingActionButton +导航组件KOTLIN

问题描述

菜单进行的导航工作正常,但是当我添加FAB并尝试在导航组件中使用时,它崩溃了。

这是我的bottomAppBar的xml:

Here is the xml of my bottomAppBar

我的问题是我如何才能将导航组件用于项目(收藏夹,IDD语音)?。

我用了这个。

favouritesFab.setonClickListener {
        navController.navigate(R.id.action_homeFragment_to_balanceAndRechargeFragment)
    }

如果我仅在“主页”中,则它可以正常工作,然后,如果我导航到其他菜单,它将崩溃,如果我在其他菜单中,例如:(“用法”或“帐户”),并试图单击“收藏夹”也会崩溃,结果就是这样。

java.lang.IllegalArgumentException: Navigation action/destination org.multisys.dst.stg:id/action_homeFragment_to_balanceAndRechargeFragment cannot be found from the current destination Destination(org.multisys.dst.stg:id/linkedAccountListsFragment) label=linked_accounts_list_fragment class=org.multisys.dst.ui.linkedaccounts.ui.LinkedAccountListsFragment

如何使用FAB中的项目像使用导航组件的菜单一样进行导航?

谢谢。

解决方法

它崩溃是因为您正在使用的操作是R.id.action_homeFragment_to_balanceAndRechargeFragment,并且当您尝试从除homeFragment之外的任何Fragment进行导航时,它都会崩溃,因为currentDestination是不同的(例如“用法”或“帐户”)。

因此可以通过

解决
favouritesFab.setOnClickListener {
    navController.navigate(R.id.balanceAndRechargeFragment)
}