【问题标题】:Navigation destination is unknown to this NavController?此 NavController 不知道导航目的地?
【发布时间】:2020-06-01 07:10:32
【问题描述】:

我在我的项目中使用导航图。 我的活动有工具栏和 navhostfragment

<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=".view.MainActivity">

<androidx.appcompat.widget.Toolbar
    android:id="@+id/my_toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="@color/colorPrimary"
    android:elevation="4dp"
    android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
    app:title="">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <androidx.appcompat.widget.AppCompatTextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:drawableLeft="@mipmap/ic_launcher_new_round"
            android:gravity="center"
            android:text="@string/app_name"
            android:textColor="@android:color/white"
            android:textSize="20sp"
            android:textStyle="bold"
            android:visibility="gone" />

        <androidx.appcompat.widget.AppCompatImageButton
            android:id="@+id/directory"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginRight="8dp"
            android:layout_toLeftOf="@+id/settings"
            android:background="@android:color/transparent"
            android:src="@drawable/directory"
            android:tint="@android:color/white"
            android:visibility="gone" />

        <androidx.appcompat.widget.AppCompatImageButton
            android:id="@+id/settings"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginRight="8dp"
            android:layout_toLeftOf="@+id/logout"
            android:background="@android:color/transparent"
            android:src="@drawable/settings"
            android:tint="@android:color/white" />

        <androidx.appcompat.widget.AppCompatImageButton
            android:id="@+id/logout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_marginRight="8dp"
            android:background="@android:color/transparent"
            android:src="@drawable/logout"
            android:tint="@android:color/white" />


    </RelativeLayout>
</androidx.appcompat.widget.Toolbar>

<fragment
    android:id="@+id/navigationHostFragment"
    android:name="androidx.navigation.fragment.NavHostFragment"
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:defaultNavHost="true"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/my_toolbar"
    app:navGraph="@navigation/main" />
 </androidx.constraintlayout.widget.ConstraintLayout>

在工具栏中,我有 2 个按钮设置和注销。当我在主页片段时,设置点击工作正常,但是当我在任何其他片段上时,点击设置会给我例外。

我的导航图如下

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

<fragment
    android:id="@+id/homeFragment"
    android:name="com.mountmeru.view.HomeFragment"
    android:label="fragment_home"
    tools:layout="@layout/fragment_home" >
    <action
        android:id="@+id/action_homeFragment_to_notificationFragment"
        app:destination="@id/notificationFragment" />
    <action
        android:id="@+id/action_homeFragment_to_categoryFragment"
        app:destination="@id/categoryFragment" />

    <action
        android:id="@+id/action_homeFragment_to_settingsFragment"
        app:destination="@id/settingsFragment" />
</fragment>
<fragment
    android:id="@+id/settingsFragment"
    android:name="com.mountmeru.view.settings.SettingsFragment"
    android:label="fragment_settings"
    tools:layout="@layout/fragment_settings" >
    <action
        android:id="@+id/action_settingsFragment_to_profileFragment"
        app:destination="@id/profileFragment" />
    <action
        android:id="@+id/action_settingsFragment_to_resetPasswordFragment"
        app:destination="@id/resetPasswordFragment" />
</fragment>

设置活动中的点击代码

  navigationController.navigate(
            R.id.action_homeFragment_to_settingsFragment)

从notificationFragment 中单击时出现异常

 java.lang.IllegalArgumentException: navigation destination com.mountmeru:id/action_homeFragment_to_settingsFragment is unknown to this NavController

【问题讨论】:

    标签: android navigation fragment


    【解决方案1】:

    您声明一个操作只是为了从HomeFragment 导航到SettingsFragment。此操作只能以 HomeFragment 为起点使用,因此对于其他 Fragments,您会收到异常“{actionId} is unknown to this NavController”。

    由于您需要从多个起点导航到SettingsFragment,您可以声明一个global action

    <action android:id="@+id/action_global_settingsFragment"
          app:destination="@id/settingsFragment"/>
    

    【讨论】:

      猜你喜欢
      • 2020-03-01
      • 2018-12-06
      • 2020-08-24
      • 1970-01-01
      • 2020-08-14
      • 2019-01-24
      • 2020-06-20
      • 1970-01-01
      • 2020-08-08
      相关资源
      最近更新 更多