NavController 在方向改变时崩溃

jus*_*eko 3 android kotlin android-jetpack android-jetpack-navigation

我不确定我在这里做错了什么。简单的设置,单个活动,片段由内部底部栏控制,到目前为止一切顺利。开始片段有一个按钮,它应该导航到另一个片段。

这是我的片段类中按钮的 onclicklistener:

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    add_step_fab.setOnClickListener {
        Navigation.findNavController(view).navigate(R.id.fragmentAtoB)
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我在 navigation.xml 中的操作:

<fragment android:id="@+id/navigation_main" android:name="com.test.test.fragments.AFragment"
          android:label="Fragment A" tools:layout="@layout/fragment_a">
    <action android:id="@+id/fragmentAtoB"
            app:destination="@id/fragmentB" app:enterAnim="@anim/nav_default_enter_anim"
            app:exitAnim="@anim/nav_default_exit_anim" app:popEnterAnim="@anim/nav_default_pop_enter_anim"
            app:popExitAnim="@anim/nav_default_pop_exit_anim"/>
</fragment>
Run Code Online (Sandbox Code Playgroud)

导航有效,但我得到了标准

java.lang.IllegalArgumentException: navigation destination com.test.test:id/fragmentAtoB is unknown to this NavController
Run Code Online (Sandbox Code Playgroud)

在旋转屏幕、拆分应用程序或有时看似随机后单击按钮时出错。貌似跟配置变化有关系?

编辑:我也尝试使用

val clickListener: View.OnClickListener = Navigation.createNavigateOnClickListener(R.id.fragmentAtoB)
    add_step_fab.setOnClickListener(clickListener)
Run Code Online (Sandbox Code Playgroud)

与上述相同的问题。

检查我是否真的在同一个片段中,正如一些为具有相同异常(但出于无关原因)的用户所建议的那样,例如:

if (controller.currentDestination?.id == R.id.navigation_main) {
            controller.navigate(R.id.fragmentAtoB)
        }
Run Code Online (Sandbox Code Playgroud)

也没有帮助。

tfr*_*ger 6

在应用程序正常运行数月后,我最近也遇到了这种情况。我最初为使用“MainActivity”类的应用程序添加了导航,然后还有其他可从菜单启动的活动(而不是让导航组件在单个活动的片段之间导航)。

所以应用程序的组织方式是 MainActivity 是“中心”,菜单选项中的选择会导致其他活动被启动,当用户从它们返回时,他们又回到了 MainActivity - 所以一个纯粹的“中心(MainActivity)和发言(其他 5 项活动)”模型。

在 MainActivity.onCreate() 中称为 setupNavigation() 方法,最初看起来像:

private void setupNavigation() {
    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    drawerLayout = findViewById(R.id.drawer_layout);

    // This class implements the listener interface for the NavigationView, handler is below.
    NavigationView navigationView = findViewById(R.id.nav_view);
    navController = Navigation.findNavController(this, R.id.nav_host_fragment);
    NavigationUI.setupActionBarWithNavController(this, navController, drawerLayout);
    NavigationUI.setupWithNavController(navigationView, navController);

    // MainActivity implements the listener for navigation events
    navigationView.setNavigationItemSelectedListener(this);

}
Run Code Online (Sandbox Code Playgroud)

正如我所提到的,直到最近,即使配置更改,这也能正常工作。即使只是将 MainActivity 上的配置从纵向更改为横向也会导致异常:

ComponentInfo{com.reddragon.intouch/com.reddragon.intouch.ui.MainActivity}: java.lang.IllegalStateException: You must call setGraph() before calling getGraph()
Run Code Online (Sandbox Code Playgroud)

我发现我必须添加这一行:

navController.setGraph(R.navigation.nav_host);
Run Code Online (Sandbox Code Playgroud)

在 Naviation.findNavController() 调用之后,然后它处理配置更改就好了。

  • 将“setGraph”方法与导航文件一起使用也解决了我的问题! (2认同)