android - 此 NavController 未知导航目的地
问题描述
When I clicked on the button this thing is happening
navigation destination com.android.example.cameraxbasic:id/action_camera_to_gallery is unknown to this NavController
这是我在片段中的代码
container.findViewById<ImageButton>(R.id.photo_view_button).setOnClickListener {
Navigation.findNavController(requireActivity(), R.id.fragment_container).navigate(
CameraFragmentDirections.actionCameraToGallery(outputDirectory.absolutePath))
}
解决方案
就我而言,我在 HomeFragment 中使用具有 FragmentOne 和 FragmentTwo 的 HomeFragment
<?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=".HomeFragment">
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="729dp"
android:fillViewport="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<androidx.fragment.app.FragmentContainerView
android:id="@+id/fragmentOne"
android:name="com.example.FragmentOne"
android:layout_width="match_parent"
android:layout_height="100dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.fragment.app.FragmentContainerView
android:id="@+id/fragmentTwo"
android:name="com.example.FragmentTwo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/fragmentCarosuel" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.core.widget.NestedScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
我的 nav_main.xml 文件如下所示:
<?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/mobile_navigation"
app:startDestination="@+id/nav_home">
<fragment
android:id="@+id/nav_home"
android:name="com.example.HomeFragment"
android:label="@string/menu_home"
tools:layout="@layout/fragment_home">
<action
android:id="@+id/action_main_to_fragment_three"
app:destination="@id/fragmentThree" />
</fragment>
<fragment
android:id="@+id/fragmentThree"
android:name="com.example.FragmentThree"
android:label="fragment_three"
tools:layout="@layout/fragment_three" >
</fragment>
</navigation>
现在,我正在从 FragmentTwo 导航任何点击事件等:
注意:请确保在 navGraph 中选择导航目的地。这里我使用两个子片段(FragmentOne 和 FragmentTwo),但我的基本片段是 HomeFragment。所以我必须从 HomeFragment 采取行动。如果有人从 FragmentOne 创建目的地,则无法从 HomeFragment 访问,并且 NavController 无法识别。
HomeFragmentDirections.ActionMainToFragmentThree nextAction = HomeFragmentDirections.actionMainToFragmentThree();
NavController navController = NavHostFragment.findNavController(this);
navController.navigate(nextAction);
抱歉英语不好。谢谢
推荐阅读
- mysql - 如何在 RMySQL 上做累计和?在 phpMyAdmin 中工作,但在 R 中给出错误
- ios - Swift:“数据”类型的值错误以键入“数据”
- git - 执行 git clone 时“过滤内容”是什么意思?
- php - 在 Woocommerce 中检索产品图库图像缩略图
- php - 当特定产品在 Woocommerce 的购物车中时,将加拿大从国家/地区列表中删除
- r - 当我尝试运行 for 循环时,为什么 R 会删除我文件中的所有信息?
- javascript - 停止输入重新渲染 onChange
- css - CSS中的自动幻灯片过渡循环
- ssl - 如果是自签名证书,浏览器是否不验证数字签名
- c# - 如何将 C++ 控制台参数发送到 C# DLL