简体   繁体   English

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

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

I have an activity ( main ) with three fragments (first, second and third).我有一个包含三个片段(第一个、第二个和第三个)的活动( main )。 I included the 3 fragments in my activity ( activity_main.xml ) by using <include layout="@layout/content_main"/> .我使用<include layout="@layout/content_main"/>在我的活动( activity_main.xml )中包含了 3 个片段。 The content_main.xml is using FragmentContainerView with id = nav_host_fragment . content_main.xml正在使用id = nav_host_fragmentFragmentContainerView And this is my nav_graph.xml :这是我的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>

I have a floatingactionbutton in my activity ( first fragment starts first) and when I click on it, I open the third fragment .我的活动中有一个floatingactionbutton操作按钮( first fragment首先开始),当我点击它时,我打开third fragment On the third fragment I have a button (next) to navigate to the first fragment , and when I click on it, I am back to first fragment using:third fragment我有一个按钮(下一步)导航到第first fragment ,当我点击它时,我回到first fragment使用:

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

Now (while I am in the first fragment ), I click on the button next (another button to navigate to the second fragment ), then the app crashes.现在(当我在第first fragment中时),我单击next按钮(另一个按钮导航到second fragment ),然后应用程序崩溃。 I found this error:我发现了这个错误:

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

Why I am getting this error?为什么我收到此错误? -I tried these suggestions here , without success. -我在这里尝试了这些建议,但没有成功。

I am using Java.我正在使用 Java。

EDIT: Read the last comment with @Zain to know why I got the error.编辑:阅读@Zain的最后一条评论,了解我收到错误的原因。

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

Bu using Navigation Architecture components, The NavController is the responsible for fragment transaction and managing the back stack instead of the Support FragmentManager .使用 Navigation Architecture 组件时,NavController 负责片段事务和管理后台堆栈,而不是 Support FragmentManager

So, instead of making tranditional framgnet transactions with FragmentManager因此,不要使用FragmentManager进行传统的 framgnet 事务

You can move from ThridFragment to the first one by:您可以通过以下方式从 ThridFragment 移动到第一个:

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

Where action_ThirdFragment_to_FirstFragment is the id of the action you defined in the navigation graph to move from ThridFragment to FirstFragment其中action_ThirdFragment_to_FirstFragment是您在导航图中定义的从ThridFragment移动到FirstFragment的操作的 ID

UPDATE:更新:

As discussed from comments, besides replacing FragmentManager by NavController in all actions;正如评论中所讨论的,除了在所有操作中用NavController替换FragmentManager there is another issue:还有一个问题:

Missing of action_FirstFragment_to_ThirdFragment action from the navigation graph.导航图中缺少action_FirstFragment_to_ThirdFragment操作。

when using the Navigation Component you should not handle the transactions yourself, instead you define the actions between each fragment and then access those directly like this使用导航组件时,您不应自己处理事务,而是定义每个片段之间的操作,然后像这样直接访问这些操作

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

and your nav_graph should be like this你的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>
   val navController  =Navigation.findNavController(requireActivity(),R.id.fragment_container)
   navigate.navigate(R.id.fragment)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM