does not have a NavController set on
原创
©著作权归作者所有:来自51CTO博客作者mp624183768的原创作品,请联系作者获取转载授权,否则将追究法律责任
Caused by: java.lang.IllegalStateException: Activity com.anguomob.jecpack.activity.NavigationActivity@c424fdc does not have a NavController set on 2131231257
两种解决思路
方案1
将
<androidx.fragment.app.FragmentContainerView 替换为
<fragment
<?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=".activity.NavigationActivity">
<fragment
android:id="@+id/fragmentContainerView"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="0dp"
android:layout_height="0dp"
app:defaultNavHost="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="@navigation/my_nav_graph" />
</androidx.constraintlayout.widget.ConstraintLayout>
方案2
将
//fragment的切换 通过NavController进行管理
val findNavController: NavController =
Navigation.findNavController(this, R.id.fragmentContainerView)
NavigationUI.setupActionBarWithNavController(this, navController = findNavController)
初始化代码 改为
val navHostFragment = supportFragmentManager
.findFragmentById(R.id.fragmentContainerView) as NavHostFragment
val navController = navHostFragment.navController
NavigationUI.setupActionBarWithNavController(this, navController = navController)