2016-04-14 18 views
5

Sto tentando di utilizzare SupportMapFragment all'interno di un frammento. Al primo avvio, la mappa viene visualizzata senza errori.Android SupportMapFragment inside Fragment

Ma quando eseguo una transazione su un altro frammento, e dopo di ciò ritorno alla mappa, ho un errore che gonfia il frammento. Ma non ho alcun errore quando faccio una nuova istanza del frammento che contiene la mappa.

Ecco il mio 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> 

E io instanciate mia SupportMapFragment in onCreateView come questo:

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

e chiamo onMapReady in onActivityCreated: registro

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

errore:

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.tapptic.collecto.ui.HomeActivity.onBackPressed(HomeActivity.java:71) 

So che non è una buona cosa per codificare un frammento all'interno del layout di un frammento, ma ho già provato a eseguire questa utilizzando un frameLayout ma ottengo molti altri errori

+0

errore postale registra anche –

+0

ho modificato il mio post –

risposta

3

Se si desidera avere mappa in frammento, è necessario utilizzare MapView invece di SupportMapFragment How to use MapView in android using google map V2?

+0

Th ank te! Questo risolve il mio bug :) –

+0

Prego) –

+1

Non è obbligatorio avere MapView. Vedi risposta di lavoro a http://stackoverflow.com/a/42160946/3496570 – Nepster

2

Opere nel 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) 
    } 
+0

Fantastico, lo proverò, grazie! –

+0

Stavo importando il riferimento al frammento della mappa direttamente prima di chiamare 'getMapAsync()', ma questo ha causato un crash in fase di esecuzione (gettandolo su FrameLayout?). L'unica cosa che ha funzionato per me era al di sopra della soluzione, usando 'childFragmentManager' per trovare il frammento. – jhauberg