android - NavHostFragment 应用程序:navGraph 需要“字符串”资源而不是“导航”资源
问题描述
我正在使用 Android Studio 3.4.2 并尝试在我的应用中实现一些基本的导航功能。
我正在按照官方文档设置 a NavHostFragement
(通过 a FragmentContainerView
)并将适当的图表链接到它。但是,当我这样做时,我在app:navGraph
属性上收到一个错误,说明:
意外的资源类型:“导航”预期:字符串。
这会导致应用程序在启动时崩溃,并且它nav_graph
本身会发出警告,表明它未链接到主机。
主要活动:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.fragment.app.FragmentContainerView
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginTop="8dp"
app:defaultNavHost="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/toolbar"
app:navGraph="@navigation/nav_graph" />
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="358dp"
android:layout_height="64dp"
android:layout_marginStart="8dp"
android:layout_marginBottom="696dp"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:theme="?attr/actionBarTheme"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0" />
</androidx.constraintlayout.widget.ConstraintLayout>
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/home_screen">
<fragment
android:id="@+id/home_screen"
android:name="com.example.ordersysteem2.home_screen"
android:label="home_screen" >
<action
android:id="@+id/action_home_screen_to_prepare"
app:destination="@id/prepare" />
<action
android:id="@+id/action_home_screen_to_kalkoenSalami"
app:destination="@id/kalkoenSalami" />
</fragment>
<fragment
android:id="@+id/prepare"
android:name="com.example.ordersysteem2.prepare"
android:label="fragment_prepare"
tools:layout="@layout/fragment_prepare" />
<fragment
android:id="@+id/kalkoenSalami"
android:name="com.example.ordersysteem2.kalkoenSalami"
android:label="fragment_kalkoen_salami"
tools:layout="@layout/fragment_kalkoen_salami" />
</navigation>
解决方案
推荐阅读
- javascript - jsTree:是否可以将 JSON 映射提供给树(而不是 JSON 数组)?
- lua - Lua if 返回 false 但应该返回 true
- python - 使用 Quart 的扩展 Quart-OpenAPI 进行 pytest 测试的完整示例在哪里?
- date - 如果满足条件,SharePoint 嵌套 If 语句可将日期添加到日期
- c - 如何确定 Int 是否为 16 位
- heroku - Heroku - 从终端管理不同的应用程序
- laravel-5.7 - “ReflectionException:类配置不存在”更新到 Laravel 5.7
- python - 有没有更优雅的方法来通过二维数组中的索引取值?
- javascript - 旋转方法使一切旋转
- typescript - 为什么详尽检查对联合类型的工作方式不同?