Navigation destination is unknown to this NavController?(导航目标对此NavController未知吗?)
本文介绍了导航目标对此NavController未知吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在我的项目中使用导航图。 我的活动有工具栏和导航主机片段
<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="IiZndDsKCiAgICAmbHQ7UmVsYXRpdmVMYXlvdXQKICAgICAgICBhbmRyb2lkOmxheW91dF93aWR0aD0="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="QGRyYXdhYmxlL2RpcmVjdG9yeQ=="
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="QGRyYXdhYmxlL3NldHRpbmdz"
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="QGRyYXdhYmxlL2xvZ291dA=="
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>
在工具栏中,我有两个按钮:设置和注销。当我在主片段中时,单击设置可以正常工作,但当我在任何其他片段上时,单击设置会出现异常。
我的导航图如下
<?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)
单击通知碎片时出现异常
java.lang.IllegalArgumentException: navigation destination com.mountmeru:id/action_homeFragment_to_settingsFragment is unknown to this NavController
推荐答案
您声明的操作仅用于从HomeFragment
导航到SettingsFragment
。此操作只能以HomeFragment
作为起点使用,因此对于其他Fragment
,您会收到异常"此NavController未知{ActionID}"。
由于您需要从多个起点导航到SettingsFragment
,因此可以声明一个global action
<action android:id="@+id/action_global_settingsFragment"
app:destination="@id/settingsFragment"/>
这篇关于导航目标对此NavController未知吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:导航目标对此NavController未知吗?


猜你喜欢
- MalformedJsonException:在第1行第1列路径中使用JsonReader.setLenient(True)接受格式错误的JSON 2022-01-01
- Android - 拆分 Drawable 2022-01-01
- 用 Swift 实现 UITextFieldDelegate 2022-01-01
- 如何检查发送到 Android 应用程序的 Firebase 消息的传递状态? 2022-01-01
- 想使用ViewPager,无法识别android.support.*? 2022-01-01
- Android viewpager检测滑动超出范围 2022-01-01
- android 4中的android RadioButton问题 2022-01-01
- Android - 我如何找出用户有多少未读电子邮件? 2022-01-01
- 在测试浓缩咖啡时,Android设备不会在屏幕上启动活动 2022-01-01
- 使用自定义动画时在 iOS9 上忽略 edgesForExtendedLayout 2022-01-01