Androidx Navigation : Activity does not have a NavController set on viewId

环境说明:

IDE : Android Studio 4.1.1

语言: kotlin 1.4.21

依赖库版本: Androidx Navigation 2.2.2

有如下布局文件:

<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout
         xmlns:android="http://schemas.android.com/apk/res/android"
         xmlns:app="http://schemas.android.com/apk/res-auto"
         android:layout_width="match_parent"
         android:layout_height="match_parent">
     <fragment
             android:layout_width="match_parent"
             android:layout_height="match_parent"
             android:id="@+id/my_nav_host_fragment"
             android:name="androidx.navigation.fragment.NavHostFragment"
             app:navGraph="@navigation/nav_sample"
             app:defaultNavHost="true" />
     <android.support.design.widget.NavigationView
             android:layout_width="wrap_content"
             android:layout_height="match_parent"
             android:layout_gravity="start"/>
  </androidx.drawerlayout.widget.DrawerLayout>

 问题描述:

在 Android Studio 4.1.1 版本中提示将布局文件中的 fragment 标签替换为 androidx.fragment.app.FragmentContainerView

在替换之后,运行报错: Activity does not have a NavController set on viewId 。

也就是在 Activity 中使用 Navigation.findNavController 找不到 NavController。

 无奈的解决:

忽略 Android Studio 的这项提示,将 androidx.fragment.app.FragmentContainerView 还原为 fragment 标签之后可以正常运行。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值