首页 > 解决方案 > 导航资源显示未找到 NavHostFragments

问题描述

我正在运行 Android Studio 3.6.3,我正在尝试使用作为 Android Jetpack 一部分的新导航资源。我做了Navigation Codelab来学习如何使用这个功能。

在我的项目中,我添加了一个导航资源,Android Studio 自动添加了此功能的依赖项。然后我创建了一个 Activity 布局,其中包含一个NavHostFragment

但是,当我转到导航资源时,HOST左侧部分显示No NavHostFragments found.

我试过同步 Gradle、清理和重建,但无济于事。

有趣的是,当我预览我的主要 Activity 布局时,“home destination”片段会透过NavHostFragment,所以看起来关系是在一个方向上建立的,而不是在另一个方向上建立的。

如何让我NavHostFragment出现在导航资源中?

这是我的布局 XML:

<?xml version="1.0" encoding="utf-8"?>
<layout 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"
    tools:context="my.app.MyActivity"
    >

    <data>
        <variable
            name="viewModel"
            type="my.app.MyViewModel" />
    </data>

    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >

        <androidx.core.widget.NestedScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fillViewport="true">

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

                <fragment
                    android:id="@+id/my_nav_host"
                    android:name="androidx.navigation.fragment.NavHostFragment"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    app:defaultNavHost="true"
                    app:layout_constraintLeft_toLeftOf="parent"
                    app:layout_constraintRight_toRightOf="parent"
                    app:navGraph="@navigation/central_navigation" />
            </LinearLayout>
        </androidx.core.widget.NestedScrollView>

        <com.google.android.material.appbar.AppBarLayout
            android:id="@+id/appbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/colorPrimary">

            <androidx.appcompat.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="@color/colorPrimary"
                app:layout_scrollFlags="scroll|enterAlways" />

        </com.google.android.material.appbar.AppBarLayout>

        <com.google.android.material.bottomappbar.BottomAppBar
            android:id="@+id/bottomAppBar"
            style="@style/Widget.MaterialComponents.BottomAppBar.Colored"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:background="@color/colorPrimary" />

    </androidx.coordinatorlayout.widget.CoordinatorLayout>
</layout>

标签: androidandroid-studioandroid-jetpack-navigation

解决方案


我有同样的问题。我按照此文档中的说明做了所有事情:https ://developer.android.com/guide/navigation/navigation-getting-started#add-navhost ,它在我的nav_graph.xml文件中显示“未找到 NavHostFragments”。我重新启动了 Android Studio,我的 MainActivity 现在显示为带有 NavHostFragment 的主机。


推荐阅读