I am trying to use the latest update of Nav Component in my application

where i can add dialog(BottomSheetDialogFragment) in my nav graph

nav_version = "2.1.0-alpha05"

Part of code from my nav_graph:

    <dialog
        android:id="@+id/settingFragment"
        android:name="com.andor.navigate.notepad.listing.fragment.SettingFragment"
        tools:layout="@layout/fragment_setting">
        <action
            android:id="@+id/action_settingFragment_to_confirmationFragment"
            app:destination="@id/confirmationFragment" />
    </dialog>
    <dialog
        android:id="@+id/confirmationFragment"
        android:name="com.andor.navigate.notepad.ConfirmationFragment"
        tools:layout="@layout/fragment_confirmation" />

In my Setting's Fragment i have a button(logout_btn) to trigger the action: action_settingFragment_to_confirmationFragment

设置片段的OnActivityCreated中的代码:

    logout_btn.setOnClickListener {
       Navigation.findNavController(view!!).navigate(R.id.action_settingFragment_to_confirmationFragment)
    } 

When i Click on the button there is a runtime Exception:

java.lang.IllegalStateException:视图 androidx.constraintlayout.widget.ConstraintLayout{550ae09 V.E.. .1,1-719,526}没有设置NavController 在androidx.navigation.Navigation.findNavController(Navigation.java:84) 在com.andor.navigate.notepad.listing.fragment.SettingFragment$onActivityCreated$2.onClick(SettingFragment.kt:56) 在android.view.View.performClick(View.java:7352) 在android.view.View.performClickInternal(View.java:7318) 在android.view.View.access$3200(View.java:846) 在android.view.View$PerformClick.run(View.java:27800) 在android.os.Handler.handleCallback(Handler.java:873) 在android.os.Handler.dispatchMessage(Handler.java:99) 在android.os.Looper.loop(Looper.java:214) 在android.app.ActivityThread.main(ActivityThread.java:7045) at java.lang.refect.Method.Invoke(本机方法) 在com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:965)

My aim is to open a dialog from another dialog, but for some reason my dialog is not having(missing) NavController.

我已经提到了来源code

推荐答案

A DialogFragment operates in an entirely separate window which is not in the same view hierarchy as your NavController.

You should use NavHostFragment.findNavController(this) (where this is your DialogFragment)) or the Kotlin extension findNavController() if you are using the fragment-ktx dependency which walks up the fragment hierarchy to find your NavController.

Kotlin相关问答推荐

等待下一个值时暂停Kotlin Coroutine

计算值的Elvis算子

新的jOOQ Gradle插件无法正确处理自引用关系

Kotlin 基于参数类型的返回类型推断

如何在 Kotlin 中为类方法调用传递变量

为什么Kotlin不用static inner class来实现带有object关键字的单例呢?

如何避免键盘打开时jetpack compose 内容上升

如何规避 Kotlin 的泛型类型差异约束

PRDownloader 即使在实现库后也无法工作.未知参考:Android Studio 中的 PRDownloader

Kotlin 可打包类抛出 ClassNotFoundException

Kotlin:内部类如何访问在外部类中声明为参数的变量?

Kotlin 代码是如何编译成原生代码的?

将 Gradle 子元素与 Kotlin 多平台一起使用

如何从定义它们的类外部调用扩展方法?

kotlin:扩展方法和空接收器

为什么 Kotlin Pair 中的条目不可变?

WebFlux 功能:如何检测空 Flux 并返回 404?

如何在 Gradle Kotlin DSL 中使用来自 gradle.properties 的插件版本?

Kotlin,什么时候按map授权?

Kotlin - 如果不为空,则使用修改后的 Obj props 覆盖 Obj props