【问题标题】:IllegalStateException: Activity does not have a NavController set on [duplicate]IllegalStateException:活动没有在 [重复] 上设置 NavController
【发布时间】:2021-05-24 00:34:51
【问题描述】:

我尝试编写一个 Android 应用程序,它使用来自 androidx 的导航组件、工具栏和抽屉布局从左侧滑入设置菜单。我按照指南Get started with the Navigation component 并尝试使用此指南Update UI components with NavigationUI 添加顶部应用栏和设置。

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

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 );
}

补充说明:

我已经找到了一些提示,说 androidx.fragment.app.FragmentContainerView 应该替换为 fragment。但是,这感觉不对,因为 linter 建议使用 FragmentContainerView 而不是 fragment 并且 official Android docs 也使用 FragmentContainerView

【问题讨论】:

    标签: android android-fragments navigation-drawer android-architecture-components android-architecture-navigation


    【解决方案1】:

    可以在您的活动中获取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" />
    
    

    但正如 lint 建议的那样,用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 Docs是错误的。此外,上面链接的帖子说必须使用findFragmentById(),因为findNavControlleronCreate() 中不起作用。但后者是 Google Android Docs 推荐的。
    • 这是一个错误看看这个issuetracker.google.com/issues/142847973
    猜你喜欢
    • 2018-11-03
    • 1970-01-01
    • 2021-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多