非法状态异常: Activity未设置NavController

7

我尝试编写一款Android应用程序,使用androidx的Navigation Component、一个工具栏和抽屉式布局从左侧滑出设置菜单。我遵循了指南“开始使用Navigation组件”,并尝试使用此指南“使用NavigationUI更新UI组件”添加一个顶部应用栏和一个设置。

当我的应用程序启动时,会抛出以下异常:

java.lang.IllegalStateException: Activity ....MainActivity@e686cd8 does not have a NavController set on 2131230993

附带说明:如果我在设计模式下打开nav_graph.xml,主机窗格会报告"未找到NavHostFragments。必须在布局中的NavHostFragment中引用此导航图才能访问它。"也许这个错误和异常是相关的,并且共享相同的根本原因。但是,我的导航图确实被一个NavHostFragment引用了,如下所示。

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<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:context=".MainActivity">

  <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/main_toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        android:theme="?attr/actionBarTheme" />

    <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:navGraph="@navigation/nav_graph" />
  </LinearLayout>

  <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:menu="@menu/drawer_view"
    app:headerLayout="@layout/nav_header" />
</androidx.drawerlayout.widget.DrawerLayout>

这个布局

  • 包含一个FragmentContainerView
  • 引用了@navigation/nav_graph(下面粘贴了)
  • 包含一个NavigationView
  • 引用了@layout/nav_header(下面粘贴了)
  • 引用了@menu/drawer_view(下面粘贴了)

nav_header.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.widget.LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="192dp"
  android:background="?attr/colorPrimaryDark"
  android:gravity="bottom"
  android:padding="16dp"
  android:theme="@style/ThemeOverlay.AppCompat.Dark">

  <TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/app_name"
    android:textAppearance="@style/TextAppearance.AppCompat.Body1" />
</android.widget.LinearLayout>

drawer_view.xml:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
  <group android:checkableBehavior="single">
    <item
      android:id="@+id/nav_home"
      android:icon="@drawable/ic_home"
      android:title="@string/home"/>
    <item
      android:id="@+id/nav_settings"
      android:icon="@drawable/ic_settings"
      android:title="@string/settings" />
  </group>
</menu>

fragment_home.xml:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  tools:context=".HomeFragment"/>

这个片段被 nav_graph 引用(下面粘贴了代码),作为主页片段(起始目标)。

nav_graph.xml:

<?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_graph"
  app:startDestination="@id/homeFragment">

  <fragment
    android:id="@+id/homeFragment"
    android:name="de.mhnnet.lychee4android.HomeFragment"
    android:label="fragment_home"
    tools:layout="@layout/fragment_home" />
</navigation>

MainActivity.java:

protected void onCreate( Bundle savedInstanceState ) {
  super.onCreate( savedInstanceState );
  setContentView( R.layout.activity_main );

  NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment );
  AppBarConfiguration appBarConfiguration =
    new AppBarConfiguration.Builder(navController.getGraph()).build();
  Toolbar toolbar = findViewById( R.id.main_toolbar );
  NavigationUI.setupWithNavController( toolbar, navController, appBarConfiguration );
}

补充说明:

我已经找到一些提示称应该用 fragment 替换 androidx.fragment.app.FragmentContainerView。然而,这感觉不对,因为代码检查器建议使用 FragmentContainerView 而不是 fragment,而且官方 Android 文档也使用 FragmentContainerView

1个回答

13

如果要在您的活动中获取Nav Controller,请替换您的

    <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:navGraph="@navigation/nav_graph" />

    <fragment
        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:navGraph="@navigation/nav_graph" />

但是,正如线索建议更改片段为 FragmentContainerView 一样,请使用以下代码在您的活动中获取 Nav Controller


        NavHostFragment navHostFragment = (NavHostFragment) getSupportFragmentManager().findFragmentById(R.id.nav_host_fragment);

        if (navHostFragment != null) {

            NavController navController = navHostFragment.getNavController();

            // Setup NavigationUI here

        }


但这基本上意味着原始的Google Android文档是错误的。此外,上面链接的帖子说必须使用findFragmentById(),因为findNavControlleronCreate()中不起作用。但后者是Google Android文档推荐的。 - user2690527
1
这是一个 bug,请看这个链接:https://issuetracker.google.com/issues/142847973 - Malik Bilal

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接