我开始使用导航组件,并且我的层次结构是:
我有一个名为nav_graph
的布局,是:
<?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"
android:id="@+id/nav_graph"
app:startDestination="@id/featureHomeNavGraph">
<include
android:id="@+id/featureHomeNavGraph"
app:graphResName="feature_home_nav_graph"
app:moduleName="feature_home" />
<include
android:id="@+id/featureFavouritesNavGraph"
app:graphResName="feature_favourites_nav_graph"
app:moduleName="feature_favourites" />
...
</navigation>
然后我将布局创建为:
feature_home_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"
app:moduleName="feature_home"
app:startDestination="@id/homeFragment">
<fragment
android:id="@+id/homeFragment"
android:name="HomeFragment"
android:label="HomeFragment"
app:moduleName="feature_home" />
</navigation>
feature_favourites_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"
app:moduleName="feature_favourites"
app:startDestination="@id/favouritesFragment">
<fragment
android:id="@+id/favouritesFragment"
android:name="FavouritesFragment"
android:label="FavouritesFragment"
app:moduleName="feature_favourites" />
</navigation>
但是运行应用程序时出现此错误
错误:标记中缺少“图形”属性。
这也是
导航XML文档元素必须包含一个app:graph属性,其值以@ navigation /开头。
我缺少什么?
答案 0 :(得分:1)
问题似乎出在缺少include
属性的app:graph
个。应该是:
<include
app:graph="@id/featureHomeNavGraph"
... />
,依此类推。 Reference。