Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does my NavController cannot find an ID that I already have?

Tags:

java

android

Every time when I launch my app it crashes and Logcat says this:

Caused by: java.lang.IllegalArgumentException: ID does not reference a View inside this Activity

So I think this is saying ID is not found, but here's my Java file and XML file corresponding to each other. Java:

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

XML:

<com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/nav_view"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="0dp"
        android:layout_marginEnd="0dp"
        android:background="?android:attr/windowBackground"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:menu="@menu/bottom_nav_menu" />

    <fragment
        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_toTopOf="@id/nav_view"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.285"
        app:navGraph="@navigation/mobile_navigation" />

The Logcat says failed because the java file, ID does not reference a View inside this Activity. But I clearly typed the ID: nav_host_fragment. I don't know what's wrong and when I'm inside Android Studio and it DID NOT report ANY error. Please help.

like image 405
Robotic Reaper Avatar asked Nov 04 '19 01:11

Robotic Reaper


People also ask

What is NavController in Android?

NavController manages app navigation within a NavHost . Apps will generally obtain a controller directly from a host, or by using one of the utility methods on the Navigation class rather than create a controller directly. Navigation flows and destinations are determined by the navigation graph owned by the controller.

What is onSupportNavigateUp?

onSupportNavigateUp() This method is called whenever the user chooses to navigate Up within your application's activity hierarchy from the action bar. @Nullable ActionMode.

What is a NavHost?

The NavHostFragment for dynamic features. A host is a single context or container for navigation via a NavController . It is strongly recommended to construct the nav controller by instantiating a NavHostController , which offers additional APIs specifically for a NavHost.

Can we use navigation component for activities?

Note: If your app uses multiple activities, each activity uses a separate navigation graph. To take full advantage of the Navigation component, your app should use multiple fragments in a single activity. However, activities can still benefit from the Navigation component.


1 Answers

Navigation.findNavController relies on findViewById(), which only works after you've called setContentView() - i.e., actually added your Views to your activity.

like image 101
ianhanniballake Avatar answered Sep 19 '22 15:09

ianhanniballake