gpt4 book ai didi

android - 异常 : View does not have a navController set

转载 作者:行者123 更新时间:2023-12-04 23:38:12 26 4
gpt4 key购买 nike

在我使用 navController = Navigation.findNAvController(view) 将其托管在 Activity 标签 fragment 中的 fragment 中,应用程序因错误而崩溃:
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>

这是 Activity 中的 fragment 标签:
        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>

最佳答案

我遇到了同样的问题,直到我意识到我们必须像这样设置 navControl onPostCreate 函数

@Override
public void onPostCreate(@Nullable Bundle savedInstanceState, @Nullable PersistableBundle persistentState) {
super.onPostCreate(savedInstanceState, persistentState);

BottomNavigationView navigationView = findViewById(R.id.nav_view);


AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
R.id.navigation_home, R.id.navigation_memories, R.id.navigation_account)
.build();
NavController navController = Navigation.findNavController(this, R.id.main_fragment);
NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
NavigationUI.setupWithNavController(navigationView, navController);
}

关于android - 异常 : View does not have a navController set,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57682209/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com