1
When I clicked on the button this thing is happening 

navigation destination com.android.example.cameraxbasic:id/action_camera_to_gallery is unknown to this NavController

This is my code in fragment

container.findViewById<ImageButton>(R.id.photo_view_button).setOnClickListener {
        Navigation.findNavController(requireActivity(), R.id.fragment_container).navigate(
                CameraFragmentDirections.actionCameraToGallery(outputDirectory.absolutePath))
    }

This is a full error description

Aamil Silawat
  • 7,735
  • 3
  • 19
  • 37

1 Answers1

0

In my case , I am using HomeFragment Having FragmentOne and FragmentTwo in HomeFragment

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".HomeFragment">


    <androidx.core.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="729dp"
        android:fillViewport="true"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent" >

            <androidx.fragment.app.FragmentContainerView
                android:id="@+id/fragmentOne"
                android:name="com.example.FragmentOne"
                android:layout_width="match_parent"
                android:layout_height="100dp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

            <androidx.fragment.app.FragmentContainerView
                android:id="@+id/fragmentTwo"
                android:name="com.example.FragmentTwo"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/fragmentCarosuel" />
        </androidx.constraintlayout.widget.ConstraintLayout>

    </androidx.core.widget.NestedScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>

my nav_main.xml file is like below:

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

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

        <action
            android:id="@+id/action_main_to_fragment_three"
            app:destination="@id/fragmentThree" />
    </fragment>  


    <fragment
        android:id="@+id/fragmentThree"
        android:name="com.example.FragmentThree"
        android:label="fragment_three"
        tools:layout="@layout/fragment_three" >
    </fragment>
</navigation>

Now, I am Navigating from FragmentTwo any click event etc.:

NOTE: Please make sure while selection of navigation destingation in navGraph. Here i am using two sub fragment (FragmentOne and FragmentTwo) but my base fragment is HomeFragment. so i have to make action from HomeFragment. If anyone create destination from FragmentOne it is not accesible from HomeFragment and NavController did not identify.

HomeFragmentDirections.ActionMainToFragmentThree nextAction = HomeFragmentDirections.actionMainToFragmentThree();
        NavController navController = NavHostFragment.findNavController(this);
        navController.navigate(nextAction);

Sorry for poor English. Thanks

Bhavsar1311
  • 723
  • 6
  • 8