【问题标题】:Navigation action/destination /action_creditTabFragment_to_needHelpFragment cannot be found from the current destination Destination(creditFragment)导航操作/目的地 /action_creditTabFragment_to_needHelpFragment 无法从当前目的地 Destination(creditFragment) 中找到
【发布时间】:2021-10-05 14:39:43
【问题描述】:

我的应用程序使用导航组件,我有CreditFragment,它使用TabLayoutViewPager2,它包含CreditTabFragment,当我在CreditTabFragment 时,我想使用@987654329 导航到NeedHelpFragment @。

如何让导航控制器知道我在creditTabFragment

我尝试创建childFragmentManager 但没有成功

ClickListener of the button that navigates to NeedHelpFragment

Sweta Jain showed this error, I couldn't add Navigation.findNavController().navigate(action) from the Answer

【问题讨论】:

    标签: android kotlin android-architecture-navigation fragmentmanager android-safe-args


    【解决方案1】:

    您可以尝试这样做:

    import androidx.navigation.Navigation
    
        buttonNeedHelp.setOnClickListener {it: View!
         val action =
                 CreditTabFragmentDirections.actionCreditTabFragmentToNeedHelpFragment(args) // add args in the bracket, as needed
                Navigation.findNavController(it)
                    .navigate(action)
        Navigation.findNavController().navigate(action)
        }
    

    或者,如果您没有参数,请尝试使用导航图中的操作 ID,

    buttonNeedHelp.setOnClickListener {it: View!
            Navigation.findNavController(it)
             .navigate(R.id.action_creditTabFragment_to_needHelpFragment)
    }
    

    【讨论】:

    • “Navigation.findNavController().navigate(action)”显示错误:以下函数均不能使用提供的参数调用。
    • 如果您没有要传递的参数,请尝试第二个。
    • 嗨,仍然有错误,主要问题是我正在使用选项卡布局,而 CreditFragment(Father) 没有导航操作,CreditTabFragment(Child) 有它,所以出现的错误是 Navigation 没有找到该操作,因为即使在子片段中它也可能使用父片段管理器,我们需要解决这个问题,让他知道我们在子管理器中。
    • 尝试将您的片段添加到主导航图。
    • 嗨,这 3 个片段(CreditFragment、CreditTabFragment、NeedHelpFragment)在同一个导航图中,问题是相同的,选项卡布局,因为选项卡位于包含它们的片段内部,这最终让我总是在 Parent Fragment 中,孩子们在 Tab Layout 中,所以任何导航(无论我找到什么导航图,因为该动作在 Parent 中不存在)指出在这个父亲中不存在,因为动作在孩子中我怀疑你不能创建一个带有父片段的导航,并且它不能有一个包含其他导航的孩子。
    【解决方案2】:

    我在设置视图寻呼机适配器时使用了子片段管理器: viewPager.adapter = SectionsPageAdapter(childFragmentManager, lifecycle) 这样您就可以使用导航了,注意:导航必须在父片段中,而不是在选项卡布局中。

    【讨论】:

      猜你喜欢
      • 2021-12-19
      • 1970-01-01
      • 1970-01-01
      • 2021-01-09
      • 1970-01-01
      • 1970-01-01
      • 2020-12-27
      • 1970-01-01
      • 2021-10-15
      相关资源
      最近更新 更多