安卓FragmentContainerView报错IllegalStateException:xx does not have a NavController set

今天使用ButtomNavigationView时build过程出现了如下错误:

java.lang.RuntimeException: 
Unable to start activity ComponentInfo{XXActivity}: 
java.lang.IllegalStateException: Activity XXActivity@198a72 does not have a NavController set on 2131230893       

说没有找到controller,但我获取controller的代码似乎没有问题

//装配BottomNavigation和fragment
        BottomNavigationView bottomNavigationView = findViewById(R.id.bottomNavigationView);
        NavController navController = Navigation.findNavController(this,R.id.fragmentContainerView);
        AppBarConfiguration configuration = new AppBarConfiguration.Builder(navController.getGraph()).build();
        NavigationUI.setupActionBarWithNavController(this,navController,configuration);
        NavigationUI.setupWithNavController(bottomNavigationView,navController);
    }

查阅资料发现,问题出在xml的组件FragmentContainerView上。
由于我使用了图形化开发工具,直接拖入NavHostFragment,发现xml使用的组件不是fragment而是FragmentContainerView,代码如下:

<androidx.fragment.app.FragmentContainerView
        android:id="@+id/fragmentContainerView"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:defaultNavHost="true"
        app:layout_constraintBottom_toTopOf="@+id/bottomNavigationView"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:navGraph="@navigation/my_nav" />

问题就在这里。解决方法如下:
方法一:
修改xml文件,把FragmentContainerView改回fragment即可,修改后如下:

<fragment
        android:id="@+id/fragmentContainerView"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:defaultNavHost="true"
        app:layout_constraintBottom_toTopOf="@+id/bottomNavigationView"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:navGraph="@navigation/my_nav" />

方法二:
不修改xml文件,而是使用supportFragmentManager获取navcontroller:

	//装配BottomNavigation和fragment
	BottomNavigationView bottomNavigationView = findViewById(R.id.bottomNavigationView);
	NavHostFragment navHostFragment = (NavHostFragment) getSupportFragmentManager().findFragmentById(R.id.fragmentContainerView);
	NavController navController = navHostFragment.getNavController();
	AppBarConfiguration configuration = new AppBarConfiguration.Builder(navController.getGraph()).build();
	NavigationUI.setupActionBarWithNavController(this,navController,configuration);
	NavigationUI.setupWithNavController(bottomNavigationView,navController);

完成!

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值