IllegalStateException: Link does not have a NavController set

前端 未结 9 2193
故里飘歌
故里飘歌 2020-11-29 03:58

I\'m using Android Navigation Component for Navigation. I have a LoginFragment which has a button to transition to SignUpFragment. On clicking the button I\'m getting this e

相关标签:
9条回答
  • 2020-11-29 04:19

    UPDATED SOLUTION

    Actually, Navigation can't find NavController in FrameLayout. So replacing <FrameLayout> with <fragment> will make it work

    Add the following inside the <fragment> tag -

    android:name="androidx.navigation.fragment.NavHostFragment"
    

    After doing the changes, the code will look similar to this -

     <fragment
           android:id="@+id/fragment_container"
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           app:layout_behavior="@string/appbar_scrolling_view_behavior"
           android:name="androidx.navigation.fragment.NavHostFragment"
           app:navGraph="@navigation/main_navigation"
           app:defaultNavHost="true"/>
    
    0 讨论(0)
  • 2020-11-29 04:22

    In Java try this below line:

    Navigation.findNavController(findViewById(R.id.nav_host_fragment)).navigate(R.id.first_fragment);
    
    0 讨论(0)
  • 2020-11-29 04:23

    Use the view of fragment such as onViewCreated

     override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
    
        val navController = Navigation.findNavController(view)
    
        binding.signUpLink.setOnClickListener {
                navController.navigate(R.id.action_loginFragment_to_signUpFragment)
        }
    
    0 讨论(0)
提交回复
热议问题