java.lang.IllegalStateException ConstraintLayout 没有为片段设置 NavController

java.lang.IllegalStateException ConstraintLayout does not have a NavController set for a fragment

我有一个包含三个片段(第一、第二和第三)的 activity (main)。我使用 <include layout="@layout/content_main"/> 在我的 activity (activity_main.xml) 中包含了 3 个片段。 content_main.xml 正在使用 FragmentContainerViewid = nav_host_fragment。这是我的 nav_graph.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/FirstFragment">

    <fragment
        android:id="@+id/FirstFragment"
        android:name="com.example.makegroups.FirstFragment"
        android:label="@string/first_fragment_label"
        tools:layout="@layout/fragment_first">

        <action
            android:id="@+id/action_FirstFragment_to_SecondFragment"
            app:destination="@id/SecondFragment" />
    </fragment>

    <fragment
        android:id="@+id/SecondFragment"
        android:name="com.example.makegroups.SecondFragment"
        android:label="@string/second_fragment_label"
        tools:layout="@layout/fragment_second">

        <action
            android:id="@+id/action_SecondFragment_to_FirstFragment"
            app:destination="@id/FirstFragment" />
        <action
            android:id="@+id/action_SecondFragment_to_ThirdFragment"
            app:destination="@id/ThirdFragment" />
    </fragment>

    <fragment
        android:id="@+id/ThirdFragment"
        android:name="com.example.makegroups.ThirdFragment"
        android:label="@string/third_fragment_label"
        tools:layout="@layout/fragment_third">

        <action
            android:id="@+id/action_ThirdFragment_to_FirstFragment"
            app:destination="@id/FirstFragment" />
    </fragment>
</navigation>

我的 activity 中有一个 floatingactionbuttonfirst fragment 首先启动),当我单击它时,我会打开 third fragment。 在 third fragment 上,我有一个按钮(下一个)可以导航到 first fragment,当我点击它时,我会回到 first fragment 使用:

Fragment frg = new FirstFragment();
FragmentManager fm = requireActivity().getSupportFragmentManager();

现在(当我在 first fragment 中时),我单击按钮 next(另一个导航到 second fragment 的按钮),然后应用程序崩溃了。我发现了这个错误:

java.lang.IllegalStateException: View androidx.constraintlayout.widget.ConstraintLayout{c9572fa V.E...... ........ 0,0-1440,2112} does not have a NavController set

为什么会出现这个错误? -我尝试了这些建议 here,但没有成功。

我正在使用 Java。

编辑: 阅读 @Zain 的最后一条评论,了解我为什么会收到错误消息。

navController = Navigation.findNavController(activity, R.id.nav_host_fragment)

Bu 使用 Navigation Architecture 组件,NavController 负责片段事务和管理返回堆栈而不是 Support FragmentManager

因此,不用使用 FragmentManager

进行传统的 framgnet 交易

您可以通过以下方式从 ThridFragment 移动到第一个:

 Navigation.findNavController(requireView()).navigate(R.id.action_ThirdFragment_to_FirstFragment);

其中 action_ThirdFragment_to_FirstFragment 是您在导航图中定义的要从 ThridFragment 移动到 FirstFragment

的操作的 ID

更新:

正如评论中所讨论的,除了在所有操作中将 FragmentManager 替换为 NavController 之外;还有一个问题:

导航图中缺少 action_FirstFragment_to_ThirdFragment 操作。

使用导航组件时,您不应自己处理事务,而应定义每个片段之间的操作,然后像这样直接访问这些操作

override fun onCreate(){
  val navController = this.findNavController()
  button.setOnClickListener{
     navController.navigate(R.id.action_FirstFragment_to_SecondFragment, null)
  }
}

你的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/FirstFragment">

<fragment
    android:id="@+id/FirstFragment"
    android:name="com.example.makegroups.FirstFragment"
    android:label="@string/first_fragment_label"
    tools:layout="@layout/fragment_first">

    <action
        android:id="@+id/action_FirstFragment_to_SecondFragment"
        app:destination="@id/SecondFragment" />
</fragment>

<fragment
    android:id="@+id/SecondFragment"
    android:name="com.example.makegroups.SecondFragment"
    android:label="@string/second_fragment_label"
    tools:layout="@layout/fragment_second">

    <action
        android:id="@+id/action_SecondFragment_to_FirstFragment"
        app:destination="@id/FirstFragment" />
    <action
        android:id="@+id/action_SecondFragment_to_ThirdFragment"
        app:destination="@id/ThirdFragment" />
</fragment>

<fragment
    android:id="@+id/ThirdFragment"
    android:name="com.example.makegroups.ThirdFragment"
    android:label="@string/third_fragment_label"
    tools:layout="@layout/fragment_third">

    <action
        android:id="@+id/action_ThirdFragment_to_FirstFragment"
        app:destination="@id/FirstFragment" />
</fragment>