1

I'm using a viewPager where one tab is a list view and one tab is a Map. The MapFragment I have defined like so:

<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"

Then I have within the onViewCreated function the following line:

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

This line throws the error:

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

Note: I use the onViewCreated() function because within onCreate() I get a null pointer on "map_fragment".

My View Pager does the following to create the fragment:

 MapViewFragment.newInstance(location, selection)

I have the right manifests as well for google play and maps.

Why is my fragment being turned into a FrameLayout? Is this expected behavior?

Danny M
  • 75
  • 7
  • I doubt this is it, but what is map_fragment.id. I've never seen that. it is typically R.id.map_fragment. – Sam Mar 20 '19 at 17:28
  • Sorry it's in Kotlin – Danny M Mar 20 '19 at 17:54
  • that's ok, I only write kotlin, and i don't have the .id extension on my xml layouts, but if it works for you, then must not be relevant to your problem. My best guess would be maybe that you need to use the support version of the fragment to match. – Sam Mar 20 '19 at 20:06

1 Answers1

2

I think it's that fragment is just not supported in ViewPager. You have to use

<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"/>

Then you need to have inside of onViewCreated()

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

to show the map.

Danny M
  • 75
  • 7