无法从当前目的地找到导航操作/目的地 - 嵌套导航图
我对整个 Android 开发尤其是导航图都很陌生。我有一个主屏幕,其中有一个底部导航组件(片段 2-5)和 4 个导航到其他片段(片段 6-9)的按钮。为了让这个结构正常工作,我与同样的错误作斗争,它最终不再出现错误,尽管我不确定我是否正确“修复”了这个问题。这是因为我现在尝试用导航抽屉组件包装活动时遇到了相同的错误。 请帮助我解决此错误并正确构建我的代码。当我单击主页片段上的按钮时会发生错误(导航抽屉和底部导航目前按预期运行)。
我的 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 (负责通过按钮从 home 导航到 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>
Home 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 文件,请告诉我哪些。
I'm new to both Android development as a whole and nav graphs in particular. I have a Home screen with a bottom navigation component (fragments 2-5) and 4 buttons that navigate to other fragments (fragments 6-9). To get this structure to work I struggled with the same error and it eventually stopped giving errors, although I'm not confident that I "fixed" the issue properly. This is backed by the fact that I'm getting the same error now that I'm attempting to wrap the activity with a navigation drawer component.
Please help me resolve this error and structure my code correctly. The error occurs when I click a button on the home fragment (navigation drawer and bottom nav behave as expected, currently).
My main_navigation.xml (details omitted for brevity):
...
<!-- 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>
My home_nav_graph.xml (responsible for navigating from home to the 4 subsequent fragments through the buttons):
<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>
Home Activity .kt file:
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)
}
If you would like me to attach more Kotlin files let me know which ones.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最终解决了我自己的问题。我将所有嵌套目的地移至顶级导航图中。
Eventually solved my own problem. I moved all the nested destinations into the top level nav graph.