IllegalArgumentException:导航目标不知道此NavController

时间:2020-04-22 05:37:15

标签: android android-architecture-navigation

使用操作从DialogFragment类导航到片段目标时,导航组件出现问题。 IllegalArgumentException:当我调用

时,导航目标不知道此NavControlle发生了
findNavController().navigate(R.id.action_dialogNotifyFragment_to_subFragment)

在DialogFragment实例内部。

注意:如果我将上面的操作ID替换为目标ID(R.id.subFragment)的片段,则导航将按预期工作。但是,我想使用SafeArgs功能,并且根据文档(https://developer.android.com/guide/navigation/navigation-pass-data),它需要将一个操作传递到Navigation方法中。

nav_host_fragment:

<androidx.constraintlayout.widget.ConstraintLayout 
    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">

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

</androidx.constraintlayout.widget.ConstraintLayout>

nav_graph:

<navigation xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/nav_graph"
    app:startDestination="@id/mainFragment">

    <fragment
        android:id="@+id/mainFragment"
        android:name="com.example.navigation.MainFragment"
        android:label="MainFragment" >
        <action
            android:id="@+id/action_mainFragment_to_dialogNotifyFragment"
            app:destination="@id/dialogNotifyFragment" />
    </fragment>
    <dialog
        android:id="@+id/dialogNotifyFragment"
        android:name="com.example.navigation.DialogNotifyFragment"
        android:label="DialogNotifyFragment" >
        <action
            android:id="@+id/action_dialogNotifyFragment_to_subFragment"
            app:destination="@id/subFragment" />
    </dialog>
    <fragment
        android:id="@+id/subFragment"
        android:name="com.example.navigation.SubFragment"
        android:label="SubFragment" />
</navigation>

DialogNotifyFragment:

class DialogNotifyFragment : DialogFragment() {

        override fun onCreateView(
            inflater: LayoutInflater,
            container: ViewGroup?,
            savedInstanceState: Bundle?
        ): View? {
           return inflater.inflate(R.layout.general_dialog, container, false)
        }

        override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
            super.onViewCreated(view, savedInstanceState)

            btn.setOnClickListener {
                //crash here
                findNavController().navigate(R.id.action_dialogNotifyFragment_to_subFragment)
            }
        }

        companion object {

           fun newInstance(): DialogNotifyFragment {
                return DialogNotifyFragment()
           }
        }
    }

我使用DialogNotifyFragment.newInstance().show(requireActivity().supportFragmentManager, "dialog")来显示对话框。

谢谢您的时间。

0 个答案:

没有答案