2016-04-14 18 views
5

Ich versuche, SupportMapFragment in einem Fragment zu verwenden. Beim ersten Start wird die Karte ohne Fehler angezeigt.Android SupportMapFragment in Fragment

Aber wenn ich eine Transaktion zu einem anderen Fragment ausführen, und danach auf die Karte zurückkehren, habe ich einen Fehler beim Aufblasen von Fragment. Aber ich habe keinen Fehler, wenn ich eine neue Instanz des Fragments mache, das die Karte enthält.

Hier ist mein 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> 

Und ich instanziieren meine SupportMapFragment in onCreateView wie folgt aus:

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

Und ich rufe onMapReady in onActivityCreated:

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

Fehlerprotokoll:

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) 

Ich weiß, dass es hart Code nicht eine gute Sache ist ein Fragment innerhalb des Layouts eines Fragments, aber ich habe schon versucht, dies auszuführen, um eine frameLayout verwenden, aber ich viel mehr Fehler

+0

Beitrag Fehler protokolliert zu –

+0

ich habe bearbeitet meinen Beitrag –

Antwort

3

Wenn Sie möchten, haben Karte in Fragment, müssen Sie MapView statt SupportMapFragment How to use MapView in android using google map V2?

+0

Th Ank du! Das löst meinen Fehler :) –

+0

Gern geschehen) –

+1

MapView ist nicht zwingend erforderlich. Siehe Arbeitsantwort unter http://stackoverflow.com/a/42160946/3496570 – Nepster

2

Works 2017 (Kotlin) verwenden:

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

     val mapFragment = childFragmentManager.findFragmentById((R.id.map)) as SupportMapFragment 
     mapFragment.getMapAsync(this) 
    } 
+0

Toll, ich werde es testen, danke! –

+0

Ich habe den Verweis auf das Map-Fragment direkt vor dem Aufruf von 'getMapAsync()' importiert, aber das verursachte einen Runtime-Crash (es in FrameLayout umwandeln?). Nur das, was für mich funktionierte, war über der Lösung und benutzte 'childFragmentManager', um das Fragment zu finden. – jhauberg