Android SupportMapFragment inside Fragment
Asked Answered
C

2

7

I'm trying to use SupportMapFragment inside a fragment. On the first launch, the map is display without any error.

But when I perform a transaction to another fragment, and after that return to the map, I have an error inflating fragment. But I don't have any error when I make en new instance of the fragment that contains the map.

Here is my layout :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:animateLayoutChanges="true"
    tools:context=".ui.HomeMapFragment">

    <fragment
        android:id="@+id/map"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/footer"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:name="com.google.android.gms.maps.SupportMapFragment" />

    <FrameLayout
        android:id="@+id/footer"
        android:layout_height="110dp"
        android:layout_width="match_parent"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:background="@color/colorPrimary"/>

</RelativeLayout>

And I instanciate my SupportMapFragment in onCreateView like this :

 //Instantiate the map fragment
 mHolder.supportMapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map);

And I call onMapReady in onActivityCreated :

if (mHolder.supportMapFragment != null)
     mHolder.supportMapFragment.getMapAsync(this);

Error log :

    FATAL EXCEPTION: 
    main  Process: xxx.xxxx.xxxx, PID: 4441
android.view.InflateException: Binary XML file line #8: Error inflating class fragment 
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:763) 
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:806) 
    at android.view.LayoutInflater.inflate(LayoutInflater.java:504) 
    at uk.co.chrisjenx.calligraphy.CalligraphyLayoutInflater.inflate(CalligraphyLayoutInflater.java:60) 
    at android.view.LayoutInflater.inflate(LayoutInflater.java:414) 
    at xxx.xxxx.xxxx.ui.HomeMapFragment.onCreateView(HomeMapFragment.java:234) 
    at android.support.v4.app.Fragment.performCreateView(Fragment.java:1974) 
    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067) 
    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1252) 
    at android.support.v4.app.BackStackRecord.popFromBackStack(BackStackRecord.java:971) 
    at android.support.v4.app.FragmentManagerImpl.popBackStackState(FragmentManager.java:1670) 
    at android.support.v4.app.FragmentManagerImpl.popBackStackImmediate(FragmentManager.java:586) 
    at android.support.v4.app.FragmentActivity.onBackPressed(FragmentActivity.java:189) 
    at com.xxx.xxx.ui.HomeActivity.onBackPressed(HomeActivity.java:71)

I know that it's not a good thing to hard code a fragment inside the layout of a fragment but I've already tried to perform this using a frameLayout but I get many more error

Competition answered 14/4, 2016 at 8:54 Comment(0)
I
5

If you want to have map in fragment, you need to use MapView instead of SupportMapFragment How to use MapView in android using google map V2?

Illboding answered 14/4, 2016 at 9:49 Comment(3)
It is not compulsory to have MapView. See working answer at https://mcmap.net/q/567648/-how-to-use-supportmapfragment-inside-a-fragmentNyala
unsatisfied answer. See @Nepster comment.Utoaztecan
@Nepster answer does not work ideally. For example, it recreates SupportMapFragment each time when fragment is hidden and shown again. This drawback isn't the case with MapViewJarvey
S
5

Works in 2017 (Kotlin):

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        val mapFragment = childFragmentManager.findFragmentById((R.id.map)) as SupportMapFragment
        mapFragment.getMapAsync(this)
    }
Sundew answered 30/12, 2017 at 13:24 Comment(1)
I was importing the reference to the map fragment directly before calling getMapAsync(), but that caused a run-time crash (casting it to FrameLayout?). Only thing that worked for me was above solution, using childFragmentManager to find the fragment.Franciskus

© 2022 - 2024 — McMap. All rights reserved.