Reputation: 1
I am trying to navigate between fragments, however when I try view.findNavController in the host fragment (login fragment) inside onViewCreated I face this error:
java.lang.IllegalStateException: View android.widget.ScrollView{f13c4b8 VFED.V... ......I. 0,0-0,0 #7f09011e app:id/login_fragment_container} does not have a NavController set
This is my fragment container view inside the activity.
<androidx.fragment.app.FragmentContainerView
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:defaultNavHost="true"
app:navGraph="@navigation/nav_graph_login"
android:name="com.example.example.fragments.LoginFragment"
/>
This is LoginFragment.kt
class LoginFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
return inflater.inflate(R.layout.login_fragment, container, false)
}
private lateinit var navController: NavController
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
navController = view.findNavController()
...
P.S. I use ScrollView as the container of the fragment, I have two navigation graphs (for two activities) and I use splash screen if it is relevant.
I have tried getting the nav controller using fragment, but it did not work either. I have also tried changing scrollview to linearlayout just incase but it did not make any difference.
Upvotes: 0
Views: 61
Reputation: 1
I added
android:name="androidx.navigation.fragment.NavHostFragment"
to the fragment container view.
Upvotes: 0