13

I basically set up 3 fragments for my bottom navigation view with all linked to activity.xml

activity.xml where I put fragment tag.

<androidx.fragment.app.FragmentContainerView
    android:id="@+id/fragment"
    android:name="androidx.navigation.fragment.NavHostFragment"
    app:defaultNavHost="true"
    app:layout_constraintBottom_toTopOf="@+id/bottomNavigationView"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/imageView2"
    app:navGraph="@navigation/my_nav"
    android:layout_width="409dp"
    android:layout_height="599dp" />

My Activity.java code (specifically):

BottomNavigationView bottomNavigationView = findViewById(R.id.bottomNavigationView);
NavController navController = Navigation.findNavController(this, R.id.fragment);
NavigationUI.setupWithNavController(bottomNavigationView, navController);

The problem now is that if I run that same java code using fragment tag in xml, it runs well but suggests I use <androidx.fragment.app.FragmentContainerView(linters) but on using <androidx.fragment.app.FragmentContainerView, it displays the error in the Logcat.

Activity does not have a NavController set on

I've seen a lot of similar errors and fixes for that on this site like FragmentContainerView as NavHostFragment

But the problem now is that most of them post Kotlin codes and the few java codes I tried didn't work for me

or who can translate this code to java:

val navHostFragment = supportFragmentManager.findFragmentById(R.id.nav_host_fragment) as NavHostFragment
val navController = navHostFragment.navController
Chinez
  • 551
  • 2
  • 6
  • 29

2 Answers2

28

You can get the Nav Controller using Java like this:

final NavHostFragment navHostFragment = (NavHostFragment) getSupportFragmentManager().findFragmentById(R.id.nav_host_fragment);
final NavController navController = navHostFragment.getNavController();

And then you setup your bottom navigation like this:

NavigationUI.setupWithNavController(bottomNavigationView, navController);
kosev
  • 2,125
  • 1
  • 20
  • 18
-5

Try using include: 1.) Create a content_main using navigation host frag

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <androidx.fragment.app.FragmentContainerView
        android:id="@+id/nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:defaultNavHost="true"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.333"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:navGraph="@navigation/nav_graph" />
</androidx.constraintlayout.widget.ConstraintLayout>

Using include in activity.xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/coordinatorLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".screens.Activity">

    <include
        android:id="@+id/include"
        layout="@layout/content_main"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginTop="4dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>