gpt4 book ai didi

java.lang.IllegalStateException : does not have a NavController set on xxxxx(location address) error 错误

转载 作者:行者123 更新时间:2023-12-05 06:18:16 27 4
gpt4 key购买 nike

我在 Android 中创建一个用户主页 Activity ,当我的应用程序在登录后尝试访问 UserHome Activity 时,我的应用程序崩溃了。我无法弄清楚为什么会这样。

错误java.lang.IllegalStateException: Activity com.example.commerce.UserHome@xxxxxxx 没有在 XXXXXXXXXXX 上设置 NavController

**(X,x表示一些字母数字地址)

activity_user_home.xml

<androidx.drawerlayout.widget.DrawerLayout 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/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">

<com.google.android.material.navigation.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_user_home"
app:menu="@menu/activity_user_home_drawer" />

<include
layout="@layout/app_bar_user_home"
android:layout_width="match_parent"
android:layout_height="match_parent" />

</androidx.drawerlayout.widget.DrawerLayout>

mobile_navigation.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/mobile_navigation"
app:startDestination="@+id/nav_home"
>

<fragment
android:id="@+id/nav_home"
android:name="com.example.commerce.ui.home.HomeFragment"
android:label="@string/menu_home"
tools:layout="@layout/fragment_home">

<action
android:id="@+id/action_HomeFragment_to_HomeSecondFragment"
app:destination="@id/nav_home_second" />
</fragment>
<fragment
android:id="@+id/nav_home_second"
android:name="com.example.commerce.ui.home.HomeSecondFragment"
android:label="@string/home_second"
tools:layout="@layout/fragment_home_second">
<action
android:id="@+id/action_HomeSecondFragment_to_HomeFragment"
app:destination="@id/nav_home" />

<argument
android:name="myArg"
app:argType="string" />
</fragment>

<fragment
android:id="@+id/nav_gallery"
android:name="com.example.commerce.ui.gallery.GalleryFragment"
android:label="@string/menu_gallery"
tools:layout="@layout/fragment_gallery" />

<fragment
android:id="@+id/nav_slideshow"
android:name="com.example.commerce.ui.slideshow.SlideshowFragment"
android:label="@string/menu_slideshow"
tools:layout="@layout/fragment_slideshow" />
</navigation>

我的 UserHome 类中的 OnCreate 方法

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_user_home);

Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

toolbar.setTitle("Home");


FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});

DrawerLayout drawer = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);

navigationView.bringToFront();

mAppBarConfiguration = new AppBarConfiguration.Builder(
R.id.nav_home, R.id.nav_gallery, R.id.nav_slideshow)
.setDrawerLayout(drawer)
.build();

NavController navController = Navigation.findNavController(UserHome.this, R.id.nav_view);

NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
NavigationUI.setupWithNavController(navigationView, navController);
}

谁能解释一下是什么原因造成的。

最佳答案

你错过了这样定义你的 navHostFragment:

<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="androidx.navigation.fragment.NavHostFragment"
android:id="@+id/nav_host_fragment"
app:navGraph="@navigation/mobile_navigation" />

这个 fragment 是所有由 navController 管理的目的地 fragment 的宿主,navController 将每个 id(通过菜单)链接到它在 navGraph 中的目的地希望这能帮助您解决问题;

关于java.lang.IllegalStateException : does not have a NavController set on xxxxx(location address) error 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61302439/

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