问题描述
这是我的导航图
<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 {
...
}
我的活动中的这段代码引发异常并崩溃
val action = MapsFragmentDirections.actionMapsFragmentToStreetViewFragment()
findNavController(R.id.my_nav_host_fragment).navigate(action)
类强制转换异常StreetViewFragment无法强制转换为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"