android - 导航错误 - 此导航图未引用任何布局文件?

标签 android android-navigation

我正在尝试将导航功能应用于我的项目。

我有这个错误:

This navigation graph is not referenced to any layout files(expected to find it in at least one layout file with a NavHostFragment with app:navGraph="@navigation/@navigation" attribute
<?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/navigation"
    android:label="fragment_init"
    app:startDestination="@id/initFragment"
    >

    <fragment
        android:id="@+id/initFragment"
        android:label="fragment_init"
        tools:layout="@layout/fragment_init">
        <action
            android:id="@+id/action_initFragment_to_authenticationFragment5"
            app:destination="@id/authenticationFragment"
            />
        <action
            android:id="@+id/action_initFragment_to_settingFragment3"
            app:destination="@id/settingFragment" />
    </fragment>
    <fragment
        android:id="@+id/authenticationFragment"
        android:name="com.example.AuthenticationFragment"
        android:label="fragment_authentication"
        tools:layout="@layout/fragment_authentication" />
    <fragment
        android:id="@+id/settingFragment"
        android:name="com.example.view.main.fragment.SettingFragment"
        android:label="SettingFragment"
        tools:layout="@layout/fragment_setting" />
</navigation>

我在各处添加了该属性(导航和 fragment )。还有,在 navigation.xml 中使用的布局文件夹中的布局文件。但是没有用。

这是activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    android:background="#000"
    android:tint="#555"
    tools:context=come.example.view.main.MainActivity">

    <ImageView
        android:id="@+id/iv_flame"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerInParent="true" />

    <fragment
        android:id="@+id/nav_host_fragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:navGraph="@navigation/navigation"/>
</RelativeLayout>

最佳答案

您还没有设置您的 <fragment>正确 - 每个 <fragment>需要一个 android:name指向它正在加载的 Fragment 类。在导航的情况下,它必须引用 androidx.navigation.fragment.NavHostFragment根据 Getting Start documentation :

<fragment
    android:id="@+id/nav_host_fragment"
    android:name="androidx.navigation.fragment.NavHostFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:defaultNavHost="true"
    app:navGraph="@navigation/navigation"/>

一旦您将导航图实际绑定(bind)到 NavHostFragment ,错误就会消失。

关于android - 导航错误 - 此导航图未引用任何布局文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56213744/

相关文章:

Android - popUpWindow 中的 DatePicker

android - 在 Android 中使用导航组件时如何删除默认动画过渡?

android - 在android中获取应用程序卸载事件

java - 注册后使用 Firebase 电话号码登录

python - 是否可以使用 Python 为 iOS 和 Android 编写跨平台应用程序?

android - 从 compose 导航到 Fragment

android - 如何使用 Jetpack Navigation 从嵌套 fragment 导航到父 fragment ?

android - 使用抽屉导航或工具栏菜单动画 fragment 过渡

android - “getActionId overrides nothing” 升级到导航版本 2.4.0-alpha02 后

Android 绝对布局/参数(在运行时移动小部件)