Android 导航 StreetViewPanoramaFragment 抛出致命的 class 转换异常

Android Navigation StreetViewPanoramaFragment throws fatal class cast exception

这是我的导航图

<navigation 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:id="@+id/nav_graph"
    app:startDestination="@id/home_dest">

    <fragment
        android:id="@+id/home_dest"
        android:name="com.conkle.mark.streettour.MapsFragment"
        android:label="activity_maps"
        tools:layout="@layout/fragment_maps"/>
    <action
        android:id="@+id/action_mapsFragment_to_streetViewFragment"
        app:destination="@id/streetViewFragment" />
    <fragment
        android:id="@+id/streetViewFragment"
        android:name="com.conkle.mark.streettour.StreetViewFragment"
        android:label="fragment_street_view"
        tools:layout="@layout/fragment_street_view" >
        <action
            android:id="@+id/action_streetViewFragment_to_mapsActivity2"
            app:destination="@id/home_dest" />
    </fragment>
</navigation>

这是我的 StreetViewPanoramaFragment 签名

class StreetViewFragment @Inject constructor(): StreetViewPanoramaFragment(), OnStreetViewPanoramaReadyCallback {
...
}

我的 Activity 中的这段代码抛出异常并崩溃

val action = MapsFragmentDirections.actionMapsFragmentToStreetViewFragment()
findNavController(R.id.my_nav_host_fragment).navigate(action)

Class cast Exception StreetViewFragment cannot be cast to androidx.fragment.app.Fragment

有什么想法吗? 提前致谢,

好的,我找到了解决方案。我没有在 Kotlin 文件中扩展 StreetViewPanoramaFragment,而是在布局文件中扩展 Fragment

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/street_fragment"
android:name="com.google.android.gms.maps.StreetViewPanoramaFragment"