Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android SupportMapFragment inside Fragment

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

like image 914
Sofiane Hassaini Avatar asked Apr 14 '16 08:04

Sofiane Hassaini


1 Answers

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)
    }
like image 126
Andrii Kovalchuk Avatar answered Sep 30 '22 23:09

Andrii Kovalchuk