【问题标题】:Navigation Resource displays No NavHostFragments Found导航资源显示未找到 NavHostFragments
【发布时间】:2020-08-22 11:17:01
【问题描述】:

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

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

但是,当我转到导航资源时,左侧的 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>

【问题讨论】:

  • 请包含您的布局 XML。
  • 好点@ianhanniballake
  • 好吧,今天我的 NavHost 出现在我的 Navigation XML 中。从那时到现在发生了很多事情,所以很难知道它为什么开始工作,但我没有改变上面的布局。

标签: android android-studio android-jetpack-navigation


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2020-12-23
    • 1970-01-01
    • 2023-03-31
    • 1970-01-01
    • 2023-03-13
    • 1970-01-01
    • 2020-11-25
    • 1970-01-01
    • 2020-07-05
    相关资源
    最近更新 更多