....... does not have a NavController set

Viewed 50

When I go to fragmentr_bag using the frag function in mainActivity and then back to home_fragment And when I want to click on the digital button to go to CategorymodeFragment, this problem occurs

java.lang.IllegalStateException: View android.widget.FrameLayout{dc05058 V.E...... ........ 0,0-1080,2063} does not have a NavController set

mainActivity:

class MainActivity : AppCompatActivity() {
    lateinit var binding: ActivityMainBinding
    override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    supportActionBar?.hide()

    binding = DataBindingUtil.setContentView(this, R.layout.activity_main)

try {
  binding.re.setOnNavigationItemSelectedListener {
       if (it.itemId == R.id.home) {
            val fragment1 = homeFragment()
              frag(fragment1)

      } else if (it.itemId == R.id.bag) {
             val fragment3 = bagFragment()
             frag(fragment3)

     }   else if (it.itemId == R.id.person) {
            val fragment2 = accontFragment()
            frag(fragment2)
    }
     true
}
}catch (e: Throwable){

} }

fun frag (fragment: Fragment) {
    try {
        val manager = supportFragmentManager
        val tra = manager.beginTransaction()
        tra.replace(R.id.fragmentContainerView, fragment)
        tra.commit()

    }catch (E:Throwable){}
}

}

homefragment:

   override fun onCreateView(
       inflater: LayoutInflater, container: ViewGroup?,
      savedInstanceState: Bundle?
     ): View? {
      binding = DataBindingUtil.inflate(inflater, R.layout.fragment_home, container, false)
      binding.digital.setOnClickListener() {
            Navigation.findNavController(it).navigate(
                homeFragmentDirections.actionHomeFragmentToListFragment()
                    .setSenderCategory("electronics")
            )
        }
     return binding.root

}

mainActivity.xml

  <layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto">

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

    
  <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/re"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginEnd="16dp"
        android:layout_marginBottom="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:menu="@menu/menu" />

    <androidx.fragment.app.FragmentContainerView
        android:id="@+id/fragmentContainerView"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:defaultNavHost="true"
        app:layout_constraintBottom_toTopOf="@+id/re"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:navGraph="@navigation/navigations" />
</androidx.constraintlayout.widget.ConstraintLayout>

 </layout>

fragmenthome.xml:

     <?xml version="1.0" encoding="utf-8"?>
      <layout 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"
      >
    <data>
   </data>
   <FrameLayout
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:name="androidx.navigation.fragment.NavHostFragment"
       >
<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".fragment.homeFragment">

        <TextView
            android:id="@+id/textView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="8dp"
            android:text="clothing"
            android:textColor="#000000"
            android:textSize="12sp"
            app:layout_constraintBottom_toBottomOf="@+id/textView2"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toEndOf="@+id/textView2"
            app:layout_constraintTop_toBottomOf="@+id/mode" />

       >

        <androidx.viewpager.widget.ViewPager
            android:id="@+id/viewPager2"
            android:layout_width="409dp"
            android:layout_height="188dp"
            android:layout_marginStart="1dp"
            android:layout_marginTop="1dp"
            android:layout_marginEnd="1dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">


        </androidx.viewpager.widget.ViewPager>

        <de.hdodenhof.circleimageview.CircleImageView
            android:id="@+id/digital"
            android:layout_width="72dp"
            android:layout_height="68dp"
            android:layout_marginTop="50dp"
            android:src="@drawable/digital"
            app:civ_border_color="@color/black"
            app:civ_border_width="1dp"
            app:layout_constraintBottom_toBottomOf="@+id/mode"
            app:layout_constraintEnd_toStartOf="@+id/mode"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toEndOf="@+id/tahrir"
        app:layout_constraintTop_toBottomOf="@+id/recyclerviewProducts/>
    </androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
   </FrameLayout>
1 Answers

You need to use activity's navcontroller as navhost fragment is in activity. Library's findNavController() may not work if use FragmentContainerView or FrameLayout.You can try 2 things. You can use the below function in fragment.

  1. If you have FragmentContainerView:

    private fun findNavController():NavController? {
         val navHostFragment = (requireActivity() as? MainActivity)?.supportFragmentManager?.findFragmentById(R.id.fragmentContainerView) as? NavHostFragment
         return navHostFragment?.navController
     }
    

2)You can make it as <fragment> in xml instead of FragmentContainerView and use below function to get NavController.

 private fun findNavController() = (requireActivity() as? MainActivity)?.findNavController()

Usage:

 binding.digital.setOnClickListener() {
                findNavController()?.navigate(
                    homeFragmentDirections.actionHomeFragmentToListFragment()
                        .setSenderCategory("electronics")
                )
            }
Related