Button does not have a NavController set

Advertisements

I started writing an andoid app game but am already struggling with the navigation.

I have a Main Activity that is also the defaultNavHost of my navigation. It displays my Main Fragment, which has 3 buttons. Each should be leading to a different Fragment but that is where the problem occurs.
When clicking any of the buttons, I get the Error "MaterialButton […] does not have a NavController set."

Example of one of the onClick() methods in my mainFragment:

@Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_main, container, false);

        view.findViewById(R.id.olympia_button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Navigation.findNavController(view).navigate(R.id.nav_main_to_olympia);
            }
        });

activity_main.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/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <androidx.fragment.app.FragmentContainerView
        android:id="@+id/nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:defaultNavHost="true"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:navGraph="@navigation/nav_graph" />
</androidx.constraintlayout.widget.ConstraintLayout>

part of my nav_graph.xml

<?xml version="1.0" encoding="utf-8"?>
<navigation 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/nav_graph"
    app:startDestination="@id/mainFragment">

    <fragment
        android:id="@+id/mainFragment"
        android:name="com.example.spieleolympiade.ui.main.MainFragment"
        android:label="fragment_main"
        tools:layout="@layout/fragment_main" >
        <action
            android:id="@+id/nav_main_to_olympia"
            app:destination="@id/olympiaFragment" />
        <action
            android:id="@+id/nav_main_to_players"
            app:destination="@id/playersFragment" />
        <action
            android:id="@+id/nav_main_to_halloffame"
            app:destination="@id/hallOfFameFragment" />
    </fragment>
    <fragment
        android:id="@+id/olympiaFragment"
        android:name="com.example.spieleolympiade.ui.olympia.OlympiaFragment"
        android:label="fragment_olympia"
        tools:layout="@layout/fragment_olympia" >
        <action
            android:id="@+id/nav_olympia_to_main"
            app:destination="@id/mainFragment" />
    </fragment>

I have read online on posts such as this one that the official solution to this problem would be

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

now I don’t understand:

  1. I can’t use the getSupportFragmentManager() function in my fragment where I need it
  2. If i put that code in my mainActivity where I can use getSupportFragmentManager(), what do I then do with the navController variable that I get out of that.

>Solution :

Can you use below code for navigation from your fragment

NavHostFragment.findNavController(this).navigate(R.id. nav_main_to_olympia)

Leave a ReplyCancel reply