我正在尝试将导航
功能应用于我的项目。
我有这个错误:
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/