android - 导航资源显示未找到 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>
解决方案
我有同样的问题。我按照此文档中的说明做了所有事情:https ://developer.android.com/guide/navigation/navigation-getting-started#add-navhost
,它在我的nav_graph.xml
文件中显示“未找到 NavHostFragments”。我重新启动了 Android Studio,我的 MainActivity 现在显示为带有 NavHostFragment 的主机。
推荐阅读
- asp.net-core - 路由模板中的 ASP.NET Core 编码参数
- android-studio - Android Studio 与 Windows 上的 IntelliJ 主题相同
- c# - 在 selenium 中单击一个 Onclick 事件
- string - C#8 nullable : string.IsNullOrEmpty 不被编译器理解为有助于防止 null
- rust - 为什么我会得到“在当前范围内没有为类型 `[usize]` 找到名为 `join` 的方法”?
- angular - 哪种方式最适合延迟加载角材料模块?
- java - OpenJDK 1.8 文件系统使用率高 CPU 使用率
- java - 当我在 `layout.xml` 文件中添加工具栏时,Android Studio RecyclerView 不会显示我的数据
- javascript - Axios 获得不同的 X-CSRF 令牌,然后是 Postman
- azure-active-directory - 通过图表添加的用户未收到欢迎电子邮件