Mil*_*ček 6 android bottomnavigationview android-jetpack-navigation
我将导航库从2.3.5更新到2.4.2。更新后我开始收到此错误:java.lang.IllegalArgumentException: No destination with ID XXX is on the NavController's back stack. The current destination is null。执行以下步骤后会发生错误:FirstFragment -> SecondFragment [with another BottomNavigationView] -> ThirdFragment -> SecondFragment [crash]
我将尝试描述我正在做的事情:
我有两个片段BottomNavigationView。
class MainBottomNavFragment : Fragment() {
private lateinit var navController: NavController
override fun onStart() {
super.onStart()
if (!::navController.isInitialized) {
navController =
Navigation.findNavController(requireActivity(), R.id.mainBottomNavHostFragment)
}
binding.bottomNavigation.setupWithNavController(navController)
}
}
Run Code Online (Sandbox Code Playgroud)
布局
<?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">
<fragment
android:id="@+id/mainBottomNavHostFragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="0dp"
android:layout_height="0dp"
app:defaultNavHost="false"
app:layout_constraintBottom_toTopOf="@+id/bottomNavigation"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="@navigation/main_menu_nav_graph" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottomNavigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:menu="@menu/main_menu" />
</androidx.constraintlayout.widget.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)
主菜单导航图
<?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"
android:id="@+id/main_bottom_nav_graph"
app:startDestination="@id/first">
<fragment
android:id="@+id/first"
android:name=".FirstFragment"
android:label="FirstFragment" />
<fragment
android:id="@+id/second"
android:name=".SecondFragment"
android:label="SecondFragment" />
<fragment
android:id="@+id/third"
android:name=".ThirdFragment"
android:label="ThirdFragment">
<action
... />
</fragment>
<navigation
android:id="@+id/fourth"
app:startDestination="@id/fourthFragment">
<fragment
android:id="@+id/fourthFragment"
android:name=".FourthFragment"
android:label="FourthFragment" />
<fragment
android:id="@+id/fourthFragmentA"
android:name=".FourthFragmentA"
android:label="FourthFragmentA" />
<fragment
android:id="@+id/fourthFragmentB"
android:name=".FourthFragmentB"
android:label="FourthFragmentB" />
</navigation>
</navigation>
Run Code Online (Sandbox Code Playgroud)
class SecondFragment : Fragment() {
private lateinit var navController: NavController
override fun onStart() {
super.onStart()
if (!::navController.isInitialized) {
navController = Navigation.findNavController(requireActivity(), R.id.secondHostFragment)
val inflater = navController.navInflater
navController.setGraph(inflater.inflate(R.navigation.second_nav_graph), arguments)
}
binding.secondBottomNavigation.setupWithNavController(navController)
}
}
Run Code Online (Sandbox Code Playgroud)
布局
<?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">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/secondBottomNavigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:menu="@menu/second_menu" />
<fragment
android:id="@+id/secondHostFragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/secondBottomNavigation" />
</androidx.constraintlayout.widget.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)
第二导航图
<?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"
android:id="@+id/second_nav_graph"
app:startDestination="@id/secondA">
<fragment
android:id="@+id/secondA"
android:name=".SecondA"
android:label="SecondA">
<action
... />
</fragment>
<fragment
android:id="@+id/secondB"
android:name=".SecondB"
android:label="SecondB" />
</navigation>
Run Code Online (Sandbox Code Playgroud)
我可以有两个嵌套的 BottomNavigationView 吗?在 2.3.5 版本中一切正常
| 归档时间: |
|
| 查看次数: |
713 次 |
| 最近记录: |