旋转屏幕时,NavHostFragment错误放大类片段

时间:2019-08-14 08:01:34

标签: android kotlin navigation-architecture

我正在创建一个使用导航体系结构的应用程序,但是我的导航没有默认主机,可以由用户根据所选目的地进行设置。

这是我的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>

感谢您的帮助,谢谢!

0 个答案:

没有答案