繁体   English   中英

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

[英]Google Maps Android: java.lang.ClassCastException: android.widget.FrameLayout cannot be cast to com.google.android.gms.maps.SupportMapFragment

我正在使用viewPager,其中一个选项卡是列表视图,一个选项卡是Map。 我已经像这样定义了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"

然后我在onViewCreated函数中有以下行:

val mapFragment = fragmentManager?.findFragmentById(map_fragment.id) as SupportMapFragment

此行抛出错误:

android.widget.FrameLayout cannot be cast to com.google.android.gms.maps.SupportMapFragment

注意:我使用onViewCreated()函数,因为在onCreate()中我在“map_fragment”上得到一个空指针。

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

 MapViewFragment.newInstance(location, selection)

我有正确的清单以及谷歌播放和地图。

为什么我的片段会变成FrameLayout? 这是预期的行为吗?

我认为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"/>

然后你需要在onViewCreated()里面

  mapView = rootView.findViewById(google_map_view.id) as MapView
  mapView.getMapAsync(this)
  //most important use onCreate;
  mapView.onCreate(arguments)

显示地图。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM