IllegalStateException: Link does not have a NavController set

前端 未结 9 2231
故里飘歌
故里飘歌 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:11

    I faced the same problem. So,instead of this,

    binding.signUpLink.setOnClickListener(Navigation.createNavigateOnClickListener(R.id.action_loginFragment_to_signUpFragment, null));

    I used my NavHostFragment to find the NavHostFragment:

    Button button = (Button)findViewById(R.id.button);
    
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
    
                    Fragment navhost = getSupportFragmentManager().findFragmentById(R.id.fragment2);
                    NavController c = NavHostFragment.findNavController(navhost);
                    c.navigate(R.id.firstFragment);
    
    
                }
            });
    

    fragment2 is navhostfragmentID.

提交回复
热议问题