1

我正在创建一个使用导航架构的应用程序,但我的导航没有默认主机,并且可以根据用户选择的目的地进行设置。

所以这是我的 NavHostFragment 的设置

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        var finalHost: NavHostFragment? = null
        when (bundle) {
            REQUEST_FIRST -> {
                finalHost = NavHostFragment.create(R.navigation.navigation_first)
            }
            REQUEST_SECOND -> {
                finalHost = NavHostFragment.create(R.navigation.navigation_second)
            }
            REQUEST_THIRD -> {
                finalHost = NavHostFragment.create(R.navigation.navigation_third)
            }
        }
        supportFragmentManager.beginTransaction()
            .replace(R.id.fragmentMain, finalHost!!)
            .setPrimaryNavigationFragment(finalHost)
            .commit()
}

这是我的主要活动布局。

<?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"
    android:background="@color/black">
    <!-- some stuff -->
    <fragment
            android:id="@+id/fragmentMain"
            android:name="androidx.navigation.fragment.NavHostFragment"
            android:layout_width="0dp"
            android:layout_height="0dp"/>
</androidx.constraintlayout.widget.ConstraintLayout>

所以,我MainActivity的加载正确但是当我开始旋转设备的屏幕时,它遇到了一些错误

java.lang.RuntimeException: Unable to start activity ComponentInfo: android.view.InflateException: Binary XML file line #10: Binary XML file line #10: Error inflating class fragment

上述错误指向布局中setContentView(R.layout.activity_main)的我和 NavHostFragment <fragment>

任何帮助表示赞赏,谢谢!

4

0 回答 0