Ошибка навигации - этот график навигации не связан ни с одним файлом макета? - PullRequest
0 голосов
/ 20 мая 2019

Я пытаюсь применить функцию 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. Но не сработало.

Это 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>

1 Ответ

4 голосов
/ 20 мая 2019

Вы неправильно настроили <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"/>

Как только вы на самом деле привязываете свой навигационный график к NavHostFragment, ошибкауйдет.

...