谷歌地图Android:java.lang.ClassCastException:Android.widget.FrameLayout无法强制转换为com.Google.Android.gms.Maps.SupportMapFragment

谷歌地图Android:java.lang.ClassCastException:Android.widget.FrameLayout无法强制转换为com.Google.Android.gms.Maps.SupportMapFragment,android,android-fragments,Android,Android Fragments,我使用的是一个viewPager,其中一个选项卡是列表视图,一个选项卡是地图。我定义的MapFragment如下: <fragment xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" class="com.google.android.gms.maps.SupportMapFragment" androi

我使用的是一个viewPager,其中一个选项卡是列表视图,一个选项卡是地图。我定义的MapFragment如下:

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    class="com.google.android.gms.maps.SupportMapFragment"
    android:id="@+id/map_fragment"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
此行抛出错误:

android.widget.FrameLayout cannot be cast to com.google.android.gms.maps.SupportMapFragment
注意:我使用onViewCreated()函数是因为在onCreate()中,我在“map\u fragment”上得到一个空指针

My View Pager执行以下操作来创建片段:

 MapViewFragment.newInstance(location, selection)
我有正确的清单以及谷歌播放和地图


为什么我的片段会变成框架布局?这是预期的行为吗?

我认为是片段在ViewPager中不受支持。你必须使用

<com.google.android.gms.maps.MapView
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/google_map_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        tools:context=".MapViewFragment"/>

显示地图。

我怀疑这是不是,但什么是map\u fragment.id。我从来没见过。它通常是R.id.map_片段。抱歉,它在kotlin中。没关系,我只写kotlin,我的xml布局上没有.id扩展名,但是如果它对您有效,那么一定与您的问题无关。我最好的猜测可能是您需要使用片段的支持版本来匹配。救生员:))
<com.google.android.gms.maps.MapView
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/google_map_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        tools:context=".MapViewFragment"/>
  mapView = rootView.findViewById(google_map_view.id) as MapView
  mapView.getMapAsync(this)
  //most important use onCreate;
  mapView.onCreate(arguments)