does not have a NavController set on

Posted 安果移不动

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了does not have a NavController set on相关的知识,希望对你有一定的参考价值。

Caused by: java.lang.IllegalStateException: Activity com.anguomob.jecpack.activity.NavigationActivity@c424fdc does not have a NavController set on 2131231257

两种解决思路

方案1

<androidx.fragment.app.FragmentContainerView 替换为

<fragment

<?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=".activity.NavigationActivity">

    <fragment
        android:id="@+id/fragmentContainerView"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:defaultNavHost="true"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:navGraph="@navigation/my_nav_graph" />
</androidx.constraintlayout.widget.ConstraintLayout>

方案2 

        //fragment的切换 通过NavController进行管理
        val findNavController: NavController =
            Navigation.findNavController(this, R.id.fragmentContainerView)
        NavigationUI.setupActionBarWithNavController(this, navController = findNavController)

初始化代码 改为

     val navHostFragment = supportFragmentManager
            .findFragmentById(R.id.fragmentContainerView) as NavHostFragment
        val navController = navHostFragment.navController
        NavigationUI.setupActionBarWithNavController(this, navController = navController)

开发者涨薪指南 48位大咖的思考法则、工作方式、逻辑体系

以上是关于does not have a NavController set on的主要内容,如果未能解决你的问题,请参考以下文章

robotium之does not have a signature matching问题

RuntimeError: element 0 of tensors does not require grad and does not have a grad_fn

RuntimeError: element 0 of tensors does not require grad and does not have a grad_fn

E-03RuntimeError: element 0 of tensors does not require grad and does not have a grad_fn

.Net自带ChartControl报错:Auto interval does not have proper value

Android开发 报错: xxx does not have a NavController set on xxx