我正在运行 Android Studio 3.6.3,我正在尝试使用作为 Android Jetpack 一部分的新导航资源。我做了Navigation Codelab了解如何使用此功能。
在我的项目中,我添加了一个导航资源,Android Studio 自动添加了此功能的依赖项。然后我用 NavHostFragment
创建了一个 Activity 布局。在里面。
但是,当我转到导航资源时,HOST
左侧部分显示 No NavHostFragments found
.
我试过同步 Gradle、清理和重建,但无济于事。
有趣的是,当我预览我的主要 Activity 布局时,“home destination” fragment 通过 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 的主机。
关于android - 导航资源显示未找到 NavHostFragments,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61650534/