![](/img/trans.png)
[英]java.lang.IllegalStateException: does not have a NavController set on xxxxx(location address) error
[英]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_fragment
的FragmentContainerView
。 這是我的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.