gpt4 book ai didi

android - 将导航架构组件与抽屉导航一起使用

转载 作者:搜寻专家 更新时间:2023-11-01 09:25:10 24 4
gpt4 key购买 nike

我正在尝试使用 Navigation Architecture Component (NavHostFragment) 与 Navigation Drawer (widget.NavigationView)。我收到以下两个错误之一。

1) 从抽屉中多次选择一个项目时可能会发生这种情况:

java.lang.IllegalArgumentException: 导航目标 app.myDomain.navdrawertrials:id/action_rootFragment_to_settingsFragment 对此 NavController 未知

2) 这发生在我的真实代码库中,该代码库的设置方式与 AFAICT 下面的简化示例相同。为什么不设置当前导航节点?

java.lang.IllegalStateException: 没有当前导航节点

简化代码

主要 Activity

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.main_activity)

setupToolbar()
setupNavDrawer()
setupNavigation()
}


private fun setupToolbar() {
setSupportActionBar( toolbar )
}

private fun setupNavigation() {
val navController = findNavController( R.id.nav_host_fragment)

setupActionBarWithNavController( navController, main_activity_drawer_layout )
}

private fun setupNavDrawer() {
val toggle = ActionBarDrawerToggle(
this,
main_activity_drawer_layout,
toolbar,
R.string.navigation_drawer_open,
R.string.navigation_drawer_close)

main_activity_drawer_layout.addDrawerListener(toggle)
toggle.syncState()


nav_drawer.setNavigationItemSelectedListener {
val navController = findNavController( R.id.nav_host_fragment )

when (it.itemId) {
R.id.nav_drawer_root_menu_item -> navController.navigate(R.id.rootFragment)
R.id.nav_drawer_first_menu_item -> navController.navigate(R.id.action_rootFragment_to_firstFragment)
R.id.nav_drawer_settings_menu_item -> navController.navigate(R.id.action_rootFragment_to_settingsFragment)
}

main_activity_drawer_layout.closeDrawer(GravityCompat.START)

true
}
}

override fun onSupportNavigateUp() = findNavController(R.id.nav_host_fragment).navigateUp()
}

main_activity.xml

<android.support.v4.widget.DrawerLayout
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:id="@+id/main_activity_drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">

<include
layout="@layout/main_activity_content"
android:layout_width="match_parent"
android:layout_height="match_parent" />

<android.support.design.widget.NavigationView
android:id="@+id/nav_drawer"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_drawer_header"
app:menu="@menu/nav_drawer_menu" />

</android.support.v4.widget.DrawerLayout>

nav_drawer_menu.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android">

<item
android:id="@+id/nav_drawer_root_menu_item"
android:title="To Root" />
<item
android:id="@+id/nav_drawer_first_menu_item"
android:title="To First" />
<item
android:id="@+id/nav_drawer_settings_menu_item"
android:title="To Settings" />
</menu>

main_activity_content.xml

<android.support.design.widget.CoordinatorLayout
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=".MainActivity">

<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme">

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary" />

</android.support.design.widget.AppBarLayout>

<fragment
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:navGraph="@navigation/nav_graph"
app:defaultNavHost="true"
/>

<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
app:srcCompat="@android:drawable/ic_dialog_email" />

</android.support.design.widget.CoordinatorLayout>

导航图.xml

<navigation 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:id="@+id/nav_graph"
app:startDestination="@id/rootFragment">

<fragment
android:id="@+id/rootFragment"
android:name="app.anytune.navdrawertrials.RootFragment"
android:label="root_fragment"
tools:layout="@layout/root_fragment" >
<action
android:id="@+id/action_rootFragment_to_firstFragment"
app:destination="@id/firstFragment" />
<action
android:id="@+id/action_rootFragment_to_settingsFragment"
app:destination="@id/settingsFragment" />
</fragment>
<fragment
android:id="@+id/firstFragment"
android:name="app.anytune.navdrawertrials.FirstFragment"
android:label="first_fragment"
tools:layout="@layout/first_fragment" />
<fragment
android:id="@+id/settingsFragment"
android:name="app.anytune.navdrawertrials.SettingsFragment"
android:label="settings_fragment"
tools:layout="@layout/settings_fragment" />
</navigation>

root_fragment.xml(其他节点是类似的空 fragment ,只有一个标签)

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".RootFragment">

<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Root Fragment" />

</FrameLayout>

最佳答案

关于第一个错误,根据您的代码,当用户从抽屉中选择“第一”或“设置”时,他将使用 action_rootFragment_to_firstFragment 转移到“第一”或“设置” fragment action_rootFragment_to_settingsFragment 操作,但是如果您尝试再次从抽屉中选择“第一”或“设置”,则 firstFragment< 中没有 action_rootFragment_to_firstFragmentaction_rootFragment_to_settingsFragment 操作 或导航图中的 settingsFragment 元素。

解决办法是改:

when (it.itemId) { 
R.id.nav_drawer_root_menu_item -> navController.navigate(R.id.rootFragment)
R.id.nav_drawer_first_menu_item -> navController.navigate(R.id.action_rootFragment_to_firstFragment)
R.id.nav_drawer_settings_menu_item -> navController.navigate(R.id.action_rootFragment_to_settingsFragment)
}

到:

   when (it.itemId) { 
R.id.nav_drawer_root_menu_item -> navController.navigate(R.id.rootFragment)
R.id.nav_drawer_first_menu_item -> navController.navigate(R.id.firstFragment)
R.id.nav_drawer_settings_menu_item -> navController.navigate(R.id.settingsFragment)
}

更好的解决方案是将目的地绑定(bind)到菜单驱动的 UI 组件(在您的抽屉中),将您的菜单项 ID 更改为与目的地 ID 相同,如下所示:

<item
android:id="@+id/rootFragment"
android:title="To Root" />
<item
android:id="@+id/firstFragment"
android:title="To First" />
<item
android:id="@+id/settingsFragment"
android:title="To Settings" />

并添加

 setupWithNavController(nav_view, navController )

在你的主要 Activity 中,而不是

nav_drawer.setNavigationItemSelectedListener {
val navController = findNavController( R.id.nav_host_fragment )

when (it.itemId) {
R.id.nav_drawer_root_menu_item -> navController.navigate(R.id.rootFragment)
R.id.nav_drawer_first_menu_item -> navController.navigate(R.id.action_rootFragment_to_firstFragment)
R.id.nav_drawer_settings_menu_item -> navController.navigate(R.id.action_rootFragment_to_settingsFragment)
}

main_activity_drawer_layout.closeDrawer(GravityCompat.START)

true
}

关于android - 将导航架构组件与抽屉导航一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51269032/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com