gpt4 book ai didi

android - 我该如何修复 `Navigation action/destination cannot be found from the current destination Destination` 错误?

转载 作者:行者123 更新时间:2023-12-05 02:41:43 48 4
gpt4 key购买 nike

我正在使用底部导航

在切换画面中,还有一个打开dialog fragment的功能。

为此我还使用了导航。

这是因为,一旦此dialog 完成,data 必须传送到打开该对话框的屏幕。

为此我使用了safe args

但是我得到了和标题一样的错误。

我知道错误在哪里,但我不知道为什么会发生。

在Stack Overflow上搜索了一下,有人和我遇到过同样的问题,但似乎不是一个确切的解决方案,因为情况不同。

nav_graph

<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/calendar">
<fragment
android:id="@+id/calendar"
android:name="com.example.writeweight.fragment.CalendarFragment"
android:label="fragment_calendar"
tools:layout="@layout/fragment_calendar" >
</fragment>
<fragment
android:id="@+id/list"
android:name="com.example.writeweight.fragment.WorkoutListFragment"
android:label="fragment_workout_list"
tools:layout="@layout/fragment_workout_list" />
<fragment
android:id="@+id/write"
android:name="com.example.writeweight.fragment.WritingRoutineFragment"
android:label="WritingRoutineFragment"
tools:layout="@layout/fragment_writing_routine">
<action
android:id="@+id/action_write_to_bodyPartDialog"
app:destination="@id/bodyPartDialog" />
<argument
android:name="title"
app:argType="string"
android:defaultValue="Write" />
</fragment>
<dialog
android:id="@+id/bodyPartDialog"
android:name="com.example.writeweight.fragment.BodyPartDialogFragment"
android:label="BodyPartDialogFragment"
tools:layout="@layout/fragment_body_part_dialog">
<action
android:id="@+id/action_bodyPartDialog_to_write"
app:destination="@id/write">
</action>
</dialog>
</navigation>

MainActivity

class MainActivity : AppCompatActivity() {
private lateinit var bottomNav: BottomNavigationView
private lateinit var navController: NavController

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

bottomNav = findViewById(R.id.bottom_nav)
val navHostFragment = supportFragmentManager.findFragmentById(R.id.nav_host_container) as NavHostFragment
navController = navHostFragment.navController
bottomNav.setupWithNavController(navController)
}
}

DialogFragment

class BodyPartDialogFragment : DialogFragment() {
private lateinit var ll: LinearLayout
private lateinit var startBtn: Button
private lateinit var title: String

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val view: View = inflater.inflate(R.layout.fragment_body_part_dialog, container, false)

initView(view)
setLayout()

startBtn?.setOnClickListener { v ->
title = BodyPartCustomView.getTitle()
val action = BodyPartDialogFragmentDirections.actionBodyPartDialogToWrite(title)
findNavController()?.navigate(action) // Possibly the location of the error.
dismiss()

}
return view
}

private fun initView(view: View) {
ll = view.findViewById(R.id.ll_body_part)
startBtn = view.findViewById(R.id.start)
}

private fun setLayout() {
ll?.apply { clipToOutline = true }
}

override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val dialog:Dialog = super.onCreateDialog(savedInstanceState)
dialog.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
dialog.setCanceledOnTouchOutside(false)
return dialog
}

override fun onDismiss(dialog: DialogInterface) {
Toast.makeText(context, "CHECK", Toast.LENGTH_SHORT).show()
}
}

最佳答案

导航前先检查当前目的地即可解决

例如

fragment A、B 和 C

在单击 fragment A 中导航到 C 的按钮时从 A 导航到 B 在某些情况下可能会导致崩溃

为此,您应该首先检查当前目的地,如下所示:

if(findNavController().currentDestination?.id==R.id.AFragment)
findNavController().navigate(
AFragmentDirections.actionAFragmentToCFragment()
)

关于android - 我该如何修复 `Navigation action/destination cannot be found from the current destination Destination` 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67976599/

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