[Solved] Android Develop Error: xxx does not have a NavController set on xxx

Execute the following code in the oncreate life cycle of activity, causing XXX does not have a navcontroller set on XXX to report an error

I/art: Do partial code cache collection, code=57KB, data=58KB
I/art: After code cache collection, code=53KB, data=56KB
I/art: Increasing code cache capacity to 256KB
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.zjm.myapplication, PID: 6291
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.zjm.myapplication/com.zjm.myapplication.footNavigation.FootNavActivity}: java.lang.IllegalStateException: Activity com.zjm.myapplication.footNavigation.FootNavActivity@23141b0 does not have a NavController set on 2131230967
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2668)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2729)
        at android.app.ActivityThread.-wrap12(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1480)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6176)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:893)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:783)
     Caused by: java.lang.IllegalStateException: Activity com.zjm.myapplication.footNavigation.FootNavActivity@23141b0 does not have a NavController set on 2131230967
        at androidx.navigation.Navigation.findNavController(Navigation.java:61)
        at com.zjm.myapplication.footNavigation.FootNavActivity.onCreate(FootNavActivity.java:28)
        at android.app.Activity.performCreate(Activity.java:6692)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2621)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2729) 
        at android.app.ActivityThread.-wrap12(ActivityThread.java) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1480) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:154) 
        at android.app.ActivityThread.main(ActivityThread.java:6176) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:893) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:783) 

Layout:

<?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/activity_foot_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".footNavigation.FootNavActivity">

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottomNavigationView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:menu="@menu/menu_foot_nav" />

    <androidx.fragment.app.FragmentContainerView
        android:id="@+id/fragmentContainerView2"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:defaultNavHost="true"
        app:layout_constraintBottom_toTopOf="@+id/bottomNavigationView"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:navGraph="@navigation/nav_graph_foot" />

</androidx.constraintlayout.widget.ConstraintLayout>

code:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityFootNavBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        BottomNavigationView bottomNavigationView = findViewById(R.id.bottomNavigationView);

        NavController navController = Navigation.findNavController(this,R.id.fragmentContainerView2);
        AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(navController.getGraph()).build();
        NavigationUI.setupActionBarWithNavController(this,navController,appBarConfiguration);
        NavigationUI.setupWithNavController(bottomNavigationView,navController);
    }

Cause of problem

The reason for the problem is that the NavController corresponding to the FragmentContainerView is not built successfully in the onCreate life cycle

 

Solution:

Method 1:

It is very simple to directly obtain navcontroller in OnStart life cycle, as shown in the following code:

    @Override
    protected void onStart() {
        super.onStart();
        navController = Navigation.findNavController(this,R.id.fragmentContainerView2);
    }

Or, put the task of obtaining NavController into view and execute it after the mapping is completed

        mBinding.fragment.post(new Runnable() {
            @Override
            public void run() {
                        navController = Navigation.findNavController(this,R.id.fragmentContainerView2);

            }
        });

Method 2:

Using getSupportFragmentManager to Get

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityFootNavBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        BottomNavigationView bottomNavigationView = findViewById(R.id.bottomNavigationView);

        NavHostFragment navHostFragment = (NavHostFragment)getSupportFragmentManager().findFragmentById(R.id.fragmentContainerView2);
        NavController navController = navHostFragment.getNavController();
        AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(navController.getGraph()).build();
        NavigationUI.setupActionBarWithNavController(this,navController,appBarConfiguration);
        NavigationUI.setupWithNavController(bottomNavigationView,navController);
    }

Read More: