簡體   English   中英

java.lang.IllegalStateException ConstraintLayout 沒有為片段設置 NavController

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

我有一個包含三個片段(第一個、第二個和第三個)的活動( main )。 我使用<include layout="@layout/content_main"/>在我的活動( activity_main.xml )中包含了 3 個片段。 content_main.xml正在使用id = nav_host_fragmentFragmentContainerView 這是我的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>

我的活動中有一個floatingactionbutton操作按鈕( first 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

為什么我收到此錯誤? -我在這里嘗試了這些建議,但沒有成功。

我正在使用 Java。

編輯:閱讀@Zain的最后一條評論,了解我收到錯誤的原因。

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

使用 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

更新:

正如評論中所討論的,除了在所有操作中用NavController替換FragmentManager 還有一個問題:

導航圖中缺少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>
   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