2

I was creating a user home page activity in Android and my app crashes when the app tries to access the UserHome Activity after login. I am not able to figure out why this is happening.

Error java.lang.IllegalStateException: Activity com.example.commerce.UserHome@xxxxxxx does not have a NavController set on XXXXXXXXXXX

**(X,x denote some alphanumeric addresses)

activity_user_home.xml

<androidx.drawerlayout.widget.DrawerLayout 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/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <com.google.android.material.navigation.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_user_home"
        app:menu="@menu/activity_user_home_drawer" />

    <include
        layout="@layout/app_bar_user_home"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</androidx.drawerlayout.widget.DrawerLayout>

mobile_navigation.xml

<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/mobile_navigation"
    app:startDestination="@+id/nav_home"
    >

    <fragment
        android:id="@+id/nav_home"
        android:name="com.example.commerce.ui.home.HomeFragment"
        android:label="@string/menu_home"
        tools:layout="@layout/fragment_home">

        <action
            android:id="@+id/action_HomeFragment_to_HomeSecondFragment"
            app:destination="@id/nav_home_second" />
    </fragment>
    <fragment
        android:id="@+id/nav_home_second"
        android:name="com.example.commerce.ui.home.HomeSecondFragment"
        android:label="@string/home_second"
        tools:layout="@layout/fragment_home_second">
        <action
            android:id="@+id/action_HomeSecondFragment_to_HomeFragment"
            app:destination="@id/nav_home" />

        <argument
            android:name="myArg"
            app:argType="string" />
    </fragment>

    <fragment
        android:id="@+id/nav_gallery"
        android:name="com.example.commerce.ui.gallery.GalleryFragment"
        android:label="@string/menu_gallery"
        tools:layout="@layout/fragment_gallery" />

    <fragment
        android:id="@+id/nav_slideshow"
        android:name="com.example.commerce.ui.slideshow.SlideshowFragment"
        android:label="@string/menu_slideshow"
        tools:layout="@layout/fragment_slideshow" />
</navigation>

The OnCreate method from my UserHome Class

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_user_home);

        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        toolbar.setTitle("Home");


        FloatingActionButton fab = findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

        DrawerLayout drawer = findViewById(R.id.drawer_layout);
        NavigationView navigationView = findViewById(R.id.nav_view);

        navigationView.bringToFront();    

        mAppBarConfiguration = new AppBarConfiguration.Builder(
                R.id.nav_home, R.id.nav_gallery, R.id.nav_slideshow)
                .setDrawerLayout(drawer)
                .build();

        NavController navController = Navigation.findNavController(UserHome.this, R.id.nav_view);

        NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
        NavigationUI.setupWithNavController(navigationView, navController);
    }

Can someone please explain what's causing it.

Jester
  • 41
  • 4
  • https://stackoverflow.com/questions/50502269/illegalstateexception-link-does-not-have-a-navcontroller-set – ADM Apr 19 '20 at 09:58
  • @ADM Thanks! but it didn't help. I checked that answer already, but I really don't get that answer. I am not able to understand what's causing it. – Jester Apr 19 '20 at 10:00
  • Found this to happen if you change in content_main.xml (standard navigation graph template) to androidx.fragment.app.FragmentContainerView. The strange thing, it doesn't fail on every project you create with the standard navigation graph template. – Roar Grønmo Oct 04 '20 at 17:28

1 Answers1

1

You missed to define your navHostFragment like this:

<fragment
       android:layout_width="match_parent"
       android:layout_height="match_parent"
        android:name="androidx.navigation.fragment.NavHostFragment"
       android:id="@+id/nav_host_fragment"
       app:navGraph="@navigation/mobile_navigation" />

this fragment is the host fo all your destinations fragments managed by navController,the navController link every id (via menus) to its detination inside the navGraph hope this help you resolving your issue;