首页 > 解决方案 > 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>

标签: androidxmlnavigation

解决方案


推荐阅读