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

    mBtHome.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {



                    Navigation.findNavController(getActivity(), R.id.homePageFragment);
                }
            });  

mBtHome button inside startDestination fragment

navigation graph xml file.

<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_file"
    app:startDestination="@id/mainFragment">
    <fragment
        android:id="@+id/mainFragment"
        android:name="com.example.libin.navigationhelphertest.ui.main.MainFragment"
        android:label="main_fragment"
        tools:layout="@layout/main_fragment">
        <action
            android:id="@+id/action_mainFragment_to_homePageFragment"
            app:destination="@id/homePageFragment" />
        <action
            android:id="@+id/action_mainFragment_to_usersListFragment"
            app:destination="@id/usersListFragment" />
    </fragment>
    <fragment
        android:id="@+id/homePageFragment"
        android:name="com.example.libin.navigationhelphertest.ui.main.HomePageFragment"
        android:label="fragment_home_page"
        tools:layout="@layout/fragment_home_page" />
    <fragment
        android:id="@+id/usersListFragment"
        android:name="com.example.libin.navigationhelphertest.ui.main.UsersListFragment"
        android:label="fragment_users_list"
        tools:layout="@layout/fragment_users_list" />
</navigation>

navigation/nav_file added inside MainActivity xml file NavHostFragment alsoe included ,Also defaultNavHost= "true" added

Libin Thomas
  • 1,132
  • 13
  • 17

2 Answers2

6

As per the above code to navigate from one fragment to another on click of the button, there are many ways.

Navigation class gives you the createNavigateOnClickListner() method you just need to pass the fragment action id.

Try the below code

 mBtHome.setOnClickListener(Navigation.createNavigateOnClickListener(R.id.action_mainFragment_to_homePageFragment));
  • I've never seen any sample use `createNavigateOnClickListener`. Is that documented in JetPack? – IgorGanapolsky Feb 03 '20 at 22:48
  • Yes, it is documented in its developer's site for navigation. Please check below link https://developer.android.com/reference/androidx/navigation/Navigation –  Feb 04 '20 at 06:45
  • 1
    Why isn't that demonstrated in any Google samples, or any samples at all? – IgorGanapolsky Feb 04 '20 at 16:34
  • 1
    No, It is used in codelab code too.See the below link https://codelabs.developers.google.com/codelabs/android-navigation/#6 –  Feb 06 '20 at 06:40
  • Neither Sunflower sample, Tivi sample, ioSched sample, nor Plaid google sample demonstrates this API. Why? Also, why is this API preferable to NavGraph Directions API? – IgorGanapolsky Feb 06 '20 at 16:42
4

R.id.homePageFragment is not a NavHostFragment do it like thie

        <androidx.fragment.app.FragmentContainerView
            android:id="@+id/fragment_container_view"
            android:name="androidx.navigation.fragment.NavHostFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:defaultNavHost="true"
            app:navGraph="@navigation/motion_layout_graph" />

and call in code like this

                Navigation.findNavController(requireActivity(), R.id.fragment_container_view).navigate(R.id.show_one)