首页 > 解决方案 > Navigation Lint 错误:此导航图未从任何布局文件中引用

问题描述

我的应用程序中有 2 个模块(应用程序模块和启动模块)。我正在尝试利用<include>标签来使FTUE自包含,而不是文档建议的主导航图的一部分。

我的主导航图在 app 模块中定义,如下所示:

<?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/nav_graph"
    app:startDestination="@id/home">

  <include app:graph="@navigation/splash_nav_graph"/>

  <fragment
      android:id="@+id/home"
      android:name="com.example.Home"
      android:label="fragment_home"
      tools:layout="@layout/fragment_home">
    <action
        android:id="@+id/action_home_to_splash_nav_graph"
        app:destination="@id/splash_nav_graph"/>
  </fragment>

</navigation>

splash_nav_graph在 splash 模块中定义(app 模块显然对它有依赖):

<?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/splash_nav_graph"
    app:startDestination="@id/splashFragment">

  <fragment
      android:id="@+id/splashFragment"
      android:name="com.example.splash.SplashFragment"
      android:label="example"
      tools:layout="@layout/fragment_splash">
    <action
        android:id="@+id/action_splashFragment_to_signInFragment"
        app:destination="@id/signInFragment"
        app:enterAnim="@anim/slide_in_right"
        app:exitAnim="@anim/slide_out_left"
        app:popEnterAnim="@anim/slide_in_left"
        app:popExitAnim="@anim/slide_out_right"/>
  </fragment>

  <fragment
      android:id="@+id/signInFragment"
      android:name="com.example.splash.signin.ui.SignInFragment"
      android:label="fragment_sign_in"
      tools:layout="@layout/fragment_sign_in">
    <action
        android:id="@+id/action_signInFragment_pop"
        app:popUpTo="@id/splash_nav_graph"/>
  </fragment>
</navigation>

最后,这是导航图与应用模块中的 NavHostFragment 相关联的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    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/mainContainer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity"
    >

  <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/nav_graph"
      />

</LinearLayout>

这将运行并在流程中导航(主页-> 启动-> 登录-> 主页)产生预期的行为。但是,顶部存在以下 lint 错误splash_nav_graph

This navigation graph is not referenced from any layout files (expected to find it in at least one layout file with a NavHostFragment with app:navGraph="@navigation/splash_nav_graph" attribute). more... (⌘F1)

lint 错误所说的是真的(我没有另一个NavHostFragmentapp:navGraph="@navigation/splash_nav_graph"),但是:

我真的需要在NavHostFragment某个地方添加另一个或者这是 lint 中的错误吗?

标签: androidandroid-navigationandroid-architecture-navigationandroid-navigation-editor

解决方案


看起来这将在AS 3.6中修复


推荐阅读