无法从当前目的地找到导航 action/destination - 嵌套导航图
Navigation action/destination cannot be found from the current destination - nested nav graph
我对 Android 整体开发和特别是导航图都是新手。我有一个带有底部导航组件(片段 2-5)和 4 个导航到其他片段(片段 6-9)的按钮的主屏幕。为了让这个结构工作,我遇到了同样的错误,它最终停止了出错,尽管我不确定我是否正确地“修复”了这个问题。这是因为我现在尝试用导航抽屉组件包装 activity 时遇到同样的错误。
请帮助我解决此错误并正确构建我的代码。当我单击主页片段上的按钮时发生错误(导航抽屉和底部导航目前按预期运行)。
我的main_navigation.xml(为简洁起见省略了细节):
...
<!-- Nav graph for the 4 buttons on Home fragment-->
<include app:graph="@navigation/home_nav_graph" />
<!--start destination - also part of home_nav_graph-->
<fragment
android:id="@+id/fragment_home"
...
</fragment>
<fragment
android:id="@+id/fragment_2"
...
</fragment>
<fragment
android:id="@+id/fragment_3"
...
</fragment>
<fragment
android:id="@+id/fragment_4"
...
</fragment>
<fragment>
android:id="@+id/fragment_5"
...
</fragment>
<!--Navigation drawer-->
<fragment
android:id="@+id/fragment_10"
... />
<fragment
android:id="@+id/fragment_11"
... />
<fragment
android:id="@+id/fragment_12"
... />
</navigation>
我的home_nav_graph.xml(负责通过按钮从主页导航到后续的4个片段):
<fragment
android:id="@+id/fragment_home"
... >
<action
android:id="@+id/action_home_to_fragment6"
app:destination="@id/fragment_6" />
<action
android:id="@+id/action_home_to_fragment7"
app:destination="@id/fragment_7" />
<action
android:id="@+id/action_home_to_fragment8"
app:destination="@id/fragment_8" />
<action
android:id="@+id/action_home_to_fragment9"
app:destination="@id/fragment_9" />
</fragment>
<fragment
android:id="@+id/fragment_6"
... >
<action
android:id="@+id/action_fragment6_to_homeFragment"
app:destination="@id/fragment_home" />
</fragment>
<fragment
android:id="@+id/fragment_7"
... >
<action
android:id="@+id/action_fragment7_to_homeFragment"
app:destination="@id/fragment_home" />
</fragment>
<fragment
android:id="@+id/fragment_8"
... >
<action
android:id="@+id/action_fragment8_to_homeFragment"
app:destination="@id/fragment_home" />
</fragment>
<fragment
android:id="@+id/fragment_9"
... >
<action
android:id="@+id/action_fragment9_to_homeFragment"
app:destination="@id/fragment_home" />
</fragment>
</navigation>
首页Activity.kt文件:
class HomeActivity : AppCompatActivity() {
private lateinit var binding: HomeActivityBinding
private lateinit var navController: NavController
private lateinit var appBarConfiguration: AppBarConfiguration
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = HomeActivityBinding.inflate(layoutInflater)
val view = binding.root
setContentView(view)
// ...
// Set-up bottom navigation menu & Side menu
val navView: NavigationView = binding.navView
val bottomNavView: BottomNavigationView = findViewById(R.id.bottom_nav_view)
val drawerLayout: DrawerLayout = binding.homeLayout // from navi drawer structure
val navHostFragment = supportFragmentManager
.findFragmentById(R.id.nav_host_fragment_home_activity) as NavHostFragment
navController = navHostFragment.navController
// val navController = findNavController(R.id.nav_host_fragment_home_activity)
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
appBarConfiguration = AppBarConfiguration(
setOf(
R.id.fragment_home,
R.id.fragment_2,
R.id.fragment_3,
R.id.fragment_4,
R.id.fragment_5
), drawerLayout
)
setupActionBarWithNavController(navController, appBarConfiguration)
navView.setupWithNavController(navController)
bottomNavView.setupWithNavController(navController)
}
如果您希望我附加更多 Kotlin 文件,请告诉我哪些文件。
最终解决了我自己的问题。我将所有嵌套目的地移动到顶级导航图中。
我对 Android 整体开发和特别是导航图都是新手。我有一个带有底部导航组件(片段 2-5)和 4 个导航到其他片段(片段 6-9)的按钮的主屏幕。为了让这个结构工作,我遇到了同样的错误,它最终停止了出错,尽管我不确定我是否正确地“修复”了这个问题。这是因为我现在尝试用导航抽屉组件包装 activity 时遇到同样的错误。 请帮助我解决此错误并正确构建我的代码。当我单击主页片段上的按钮时发生错误(导航抽屉和底部导航目前按预期运行)。
我的main_navigation.xml(为简洁起见省略了细节):
...
<!-- Nav graph for the 4 buttons on Home fragment-->
<include app:graph="@navigation/home_nav_graph" />
<!--start destination - also part of home_nav_graph-->
<fragment
android:id="@+id/fragment_home"
...
</fragment>
<fragment
android:id="@+id/fragment_2"
...
</fragment>
<fragment
android:id="@+id/fragment_3"
...
</fragment>
<fragment
android:id="@+id/fragment_4"
...
</fragment>
<fragment>
android:id="@+id/fragment_5"
...
</fragment>
<!--Navigation drawer-->
<fragment
android:id="@+id/fragment_10"
... />
<fragment
android:id="@+id/fragment_11"
... />
<fragment
android:id="@+id/fragment_12"
... />
</navigation>
我的home_nav_graph.xml(负责通过按钮从主页导航到后续的4个片段):
<fragment
android:id="@+id/fragment_home"
... >
<action
android:id="@+id/action_home_to_fragment6"
app:destination="@id/fragment_6" />
<action
android:id="@+id/action_home_to_fragment7"
app:destination="@id/fragment_7" />
<action
android:id="@+id/action_home_to_fragment8"
app:destination="@id/fragment_8" />
<action
android:id="@+id/action_home_to_fragment9"
app:destination="@id/fragment_9" />
</fragment>
<fragment
android:id="@+id/fragment_6"
... >
<action
android:id="@+id/action_fragment6_to_homeFragment"
app:destination="@id/fragment_home" />
</fragment>
<fragment
android:id="@+id/fragment_7"
... >
<action
android:id="@+id/action_fragment7_to_homeFragment"
app:destination="@id/fragment_home" />
</fragment>
<fragment
android:id="@+id/fragment_8"
... >
<action
android:id="@+id/action_fragment8_to_homeFragment"
app:destination="@id/fragment_home" />
</fragment>
<fragment
android:id="@+id/fragment_9"
... >
<action
android:id="@+id/action_fragment9_to_homeFragment"
app:destination="@id/fragment_home" />
</fragment>
</navigation>
首页Activity.kt文件:
class HomeActivity : AppCompatActivity() {
private lateinit var binding: HomeActivityBinding
private lateinit var navController: NavController
private lateinit var appBarConfiguration: AppBarConfiguration
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = HomeActivityBinding.inflate(layoutInflater)
val view = binding.root
setContentView(view)
// ...
// Set-up bottom navigation menu & Side menu
val navView: NavigationView = binding.navView
val bottomNavView: BottomNavigationView = findViewById(R.id.bottom_nav_view)
val drawerLayout: DrawerLayout = binding.homeLayout // from navi drawer structure
val navHostFragment = supportFragmentManager
.findFragmentById(R.id.nav_host_fragment_home_activity) as NavHostFragment
navController = navHostFragment.navController
// val navController = findNavController(R.id.nav_host_fragment_home_activity)
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
appBarConfiguration = AppBarConfiguration(
setOf(
R.id.fragment_home,
R.id.fragment_2,
R.id.fragment_3,
R.id.fragment_4,
R.id.fragment_5
), drawerLayout
)
setupActionBarWithNavController(navController, appBarConfiguration)
navView.setupWithNavController(navController)
bottomNavView.setupWithNavController(navController)
}
如果您希望我附加更多 Kotlin 文件,请告诉我哪些文件。
最终解决了我自己的问题。我将所有嵌套目的地移动到顶级导航图中。