【发布时间】:2019-05-20 02:44:38
【问题描述】:
我正在尝试将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>
我在这里和那里添加了该属性(导航和片段)。还有,在navigation.xml 中使用的layout 文件夹中的布局文件。但是没用。
这是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>
【问题讨论】:
-
使用 fragmentcontainerview 也可以得到上述结果
标签: android android-navigation