相关疑难解决方法(0)

<androidx.fragment.app.FragmentContainerView> 与 <fragment> 作为 NavHost 的视图

androidx.fragment.app.FragmentContainerView用作 navHost 而不是常规fragment应用程序时,在方向更改后无法导航到目的地。

我收到以下错误: java.lang.IllegalStateException: no current navigation node

是否有我应该知道的正确使用它的问题,或者我使用导航组件的方式不正确?

带有视图的简单活动 xml:


...
    <androidx.fragment.app.FragmentContainerView
        android:id="@+id/nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:defaultNavHost="true"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        app:navGraph="@navigation/nav_simple" />
...
Run Code Online (Sandbox Code Playgroud)

导航代码:

<?xml version="1.0" encoding="utf-8"?>
<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_legislator.xml"
    app:startDestination="@id/initialFragment">

    <fragment
        android:id="@+id/initialFragment"
        android:name="com.example.fragmenttag.InitialFragment"
        android:label="Initial Fragment"
        tools:layout="@layout/initial_fragment">
        <action
            android:id="@+id/action_initialFragment_to_destinationFragment"
            app:destination="@id/destinationFragment" />
    </fragment>
    <fragment
        android:id="@+id/destinationFragment"
        android:name="com.example.fragmenttag.DestinationFragment"
        android:label="Destination Fragment"
        tools:layout="@layout/destination_fragment" />

</navigation>
Run Code Online (Sandbox Code Playgroud)

这是一个 github 存储库,您可以在其中轻松重现错误:https : //github.com/dmytroKarataiev/navHostBug

navigation android android-architecture-components

29
推荐指数
2
解决办法
2万
查看次数