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

c-a*_*-an 0 android android-navigation

我正在尝试将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
Run Code Online (Sandbox Code Playgroud)
<?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>
Run Code Online (Sandbox Code Playgroud)

我在各处添加了该属性(导航和片段)。另外,导航.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>
Run Code Online (Sandbox Code Playgroud)

Aug*_*sen 6

如果接受的答案最初不起作用,请尝试“制作项目”。为我工作<fragment/>


ian*_*ake 5

您尚未<fragment>正确设置-每个人都<fragment>需要android:name指向它正在加载的Fragment类。对于导航,它必须参考androidx.navigation.fragment.NavHostFragment入门指南》文档中的

<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"/>
Run Code Online (Sandbox Code Playgroud)

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

  • @iamhanniballake `&lt;fragmentcontainerview/&gt;` 也给出了上述错误,所以我目前避免使用fragmentcontainerview。解决方案是什么? (14认同)
  • 我添加了 android:name="androidx.navigation.fragment.NavHostFragment" 并且我仍然收到完全相同的错误 (11认同)