在我使用 navController = Navigation.findNAvController(view) 将其托管在活动中的标记片段中的片段中,应用程序因错误而崩溃:视图没有设置 navController。
这是导航图:
<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/mainFragment">
<fragment
android:id="@+id/mainFragment"
android:name="studio.apptick.mouj.view.fragments.MainFragment"
tools:layout="@layout/fragment_main"
android:label="fragment_main" >
<action
android:id="@+id/action_mainFragment_to_playlistActivityFragment"
app:destination="@id/playlistActivityFragment" />
<action
android:id="@+id/action_mainFragment_to_searchActivity"
app:destination="@id/searchActivity" />
</fragment>
<activity
android:id="@+id/searchActivity"
android:name="studio.apptick.mouj.view.activity.SearchActivity"
android:label="activity_search"
tools:layout="@layout/activity_search" />
</navigation>
这是活动中的片段标签:
android:id="@+id/nav_host_fragment"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="@id/view_player"
android:name="androidx.navigation.fragment.NavHostFragment"
app:defaultNavHost="true"
app:navGraph="@navigation/nav_graph">
</fragment>