【发布时间】:2021-04-10 08:16:28
【问题描述】:
我的导航非常简单,当我尝试转到新片段时出现此错误:
java.lang.IllegalArgumentException: Navigation action/destination com.my.app:id/action_settingsFragment_to_profileFragment cannot be found from the current destination Destination(com.my.app:id/mainFragment) label=fragment_main class=com.my.app.main.MainFragment
navigation
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_graphs"
app:startDestination="@id/mainFragment">
<fragment
android:id="@+id/mainFragment"
android:name="com.my.app.main.MainFragment"
android:label="fragment_main"
tools:layout="@layout/fragment_main" >
<action
android:id="@+id/action_mainFragment_to_laundriesFragment"
app:destination="@id/laundriesFragment" />
</fragment>
<!-- other fragments here... -->
<fragment
android:id="@+id/settingsFragment"
android:name="com.my.app.main.SettingsFragment"
android:label="fragment_settings"
tools:layout="@layout/fragment_settings" >
<action
android:id="@+id/action_settingsFragment_to_newOrderFragment"
app:destination="@id/newOrderFragment" />
<action
android:id="@+id/action_settingsFragment_to_profileFragment"
app:destination="@id/profileFragment" />
</fragment>
<fragment
android:id="@+id/profileFragment"
android:name="com.my.app.main.ProfileFragment"
android:label="fragment_profile"
tools:layout="@layout/fragment_profile" >
</fragment>
</navigation>
SettingsFragment.kt(重定向代码)
when (position) {
0 -> {
//....
}
1 -> {
navController.navigate(R.id.action_settingsFragment_to_profileFragment)
}
2 -> {
//...
}
}
有什么想法吗?
更新
我已经设法使用以下代码重定向到 profileFragment,但是当我点击返回按钮时,它会关闭应用程序而不是返回到 settingsFragment
private fun GenerateItems(){
when (position) {
0 -> {
//
}
1 -> {
this@SettingsFragment.navigateTo(ProfileFragment())
}
2 -> {
//
}
}
}
private fun navigateTo(fragment: Fragment) {
val transaction = (activity as FragmentActivity).supportFragmentManager
.beginTransaction()
.replace(R.id.fragmentFragmentId, fragment)
transaction.commit()
}
关于后退按钮问题的任何想法?
【问题讨论】:
-
你有没有用行动尝试过,使用方向?
-
@Eyosiyas 你是什么意思?
-
你用来传递数据或参数的那个。
-
NavDirections action = SpecifyAmountFragmentDirections .actionSpecifyAmountFragmentToConfirmationFragment(); Navigation.findNavController(view).navigate(action);
-
@Eyosiyas 不确定我是否理解正确,但我也有类似的情况
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) navController = Navigation.findNavController(view) }
标签: android kotlin android-navigation