Я пытаюсь использовать новый график навигации в android для переключения между фрагментами. Тем не менее, я получаю сообщение об ошибке:
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/nav_graph" attribute). Navigation resource files must be referenced from a NavHostFragment in a layout in order to be relevant.
Я видел много людей с одной и той же ошибкой, но, похоже, все сделали что-то не так и могли это исправить. В моем случае я почти на 100% уверен, что мой код в порядке ...
Вот мой nav_graph. xml:
<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/mainFragment">
<fragment
android:id="@+id/mainFragment"
android:name="com.ouimet.budget.MainFragment"
android:label="fragment_main"
tools:layout="@layout/fragment_main" >
<action
android:id="@+id/action_mainFragment_to_expenseFragment"
app:destination="@id/expenseFragment" />
</fragment>
<fragment
android:id="@+id/expenseFragment"
android:name="com.ouimet.budget.ExpenseFragment"
android:label="fragment_expense"
tools:layout="@layout/fragment_expense" />
</navigation>
Наряду с файлом ресурсов моей MainActivity Activity_main. xml (который ссылается на nav_graph. xml):
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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"
tools:context=".MainActivity">
<androidx.fragment.app.FragmentContainerView
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:defaultNavHost="true"
app:navGraph="@navigation/nav_graph" />
</androidx.constraintlayout.widget.ConstraintLayout>
И мой фрагмент_ *. 1026 *:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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"
tools:context=".MainFragment"
android:id="@+id/parentLayout">
<Button
android:id="@+id/view_expense_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Entrer une dépense"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toRightOf="parent"
app:layout_constraintRight_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Как мы видим, на мой nav_graph ссылаются .. Кроме того, когда я компилирую и запускаю код, переход между фрагментами работает! Это известная проблема или просто я чего-то не вижу?
Спасибо за помощь!
Александр