2013-08-02 2 views
0

Le code suivant fonctionne très bien dans une seule orientation, mais dès que l'orientation change provoque une exception:SupportMapFragment fait exception sur le changement d'orientation

using Android.App; 
using Android.Content.PM; 
using Android.Gms.Maps; 
using Android.Gms.Maps.Model; 
using Android.OS; 
using Android.Support.V4.App; 
using Cirrious.MvvmCross.Droid.Fragging; 
using System; 
using System.Collections.Generic; 
using System.Drawing; 

namespace Demo.Droid.Views 
{ 
    [Activity(Label = "View for MapViewModel", Theme = "@android:style/Theme.NoTitleBar", ConfigurationChanges = ConfigChanges.Orientation)] 
    public class MapView : MvxFragmentActivity 
    { 
     private SupportMapFragment _mapFragment; 

     protected override void OnCreate(Bundle bundle) 
     { 
      base.OnCreate(bundle); 
      SetContentView(Resource.Layout.MapView); 

      InitMapFragment(); 
     } 

     private void InitMapFragment() 
     { 
      _mapFragment = SupportFragmentManager.FindFragmentByTag("map") as SupportMapFragment; 
      if (_mapFragment == null) 
      { 
       GoogleMapOptions mapOptions = new GoogleMapOptions() 
        .InvokeMapType(GoogleMap.MapTypeNormal) 
        .InvokeZoomControlsEnabled(false) 
        .InvokeCompassEnabled(true); 

       FragmentTransaction fragTx = SupportFragmentManager.BeginTransaction(); 
       _mapFragment = SupportMapFragment.NewInstance(mapOptions); 

       fragTx.Add(Resource.Id.map, _mapFragment, "map"); 
       fragTx.Commit(); 
      } 
     } 
    } 
} 

Si je commente le InitMapFragment dans OnCreate alors les charges de carte et tourne correctement, donc il semble que ce soit quelque chose à voir avec SupportMapFragment.

Le fichier de mise en page contient:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:local="http://schemas.android.com/apk/res-auto" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="@android:color/white"> 
    <RelativeLayout 
     android:layout_width="fill_parent" 
     android:layout_height="0dp" 
     android:layout_weight="0.1" 
     android:background="@drawable/nav_bar_background" 
     android:id="@+id/bartop"> 
    </RelativeLayout> 
    <fragment 
     class="com.google.android.gms.maps.SupportMapFragment" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="0.8" 
     android:id="@+id/map" /> 
    <RelativeLayout 
     android:orientation="horizontal" 
     android:minWidth="25px" 
     android:minHeight="25px" 
     android:layout_width="fill_parent" 
     android:layout_height="0dp" 
     android:layout_weight="0.1" 
     android:id="@+id/barbottom" 
     android:layout_alignParentBottom="true" 
     android:background="@drawable/nav_bar_background"> 
    </RelativeLayout> 
</LinearLayout> 

de déboguage montre juste 'Une exception non gérée est survenue.' mais le journal de l'appareil montre:

android.view.InflateException: Binary XML file line #1: Error inflating class fragment 
(raw stack trace not found) 
Caused by: 
java.lang.IllegalStateException: Fragment com.google.android.gms.maps.SupportMapFragment did not create a view. 
+0

Quelle est l'exception? – Cheesebaron

+0

InflateException sur OnCreate. J'ai ajouté des détails supplémentaires à la question. – Neil

Répondre

1

Je ne suis toujours pas sûr de la cause de l'erreur, mais en changeant le fragment à un FrameLayout dans le fichier XML, puis en ajoutant le fragment fixe ce programme.

using Android.App; 
using Android.Content.PM; 
using Android.Gms.Maps; 
using Android.Gms.Maps.Model; 
using Android.OS; 
using Android.Support.V4.App; 
using Cirrious.MvvmCross.Droid.Fragging; 
using System; 
using System.Collections.Generic; 
using System.Drawing; 

namespace Demo.Droid.Views 
{ 
    [Activity(Label = "View for MapViewModel", Theme = "@android:style/Theme.NoTitleBar", ConfigurationChanges = ConfigChanges.Orientation)] 
    public class MapView : MvxFragmentActivity 
    { 
     private SupportMapFragment _mapFragment; 

     protected override void OnCreate(Bundle bundle) 
     { 
      base.OnCreate(bundle); 

      _mapFragment = new SupportMapFragment(); 
      var fragmentTx = this.SupportFragmentManager.BeginTransaction(); 
      fragmentTx.Add(Resource.Id.mapFrame, _mapFragment); 
      fragmentTx.Commit(); 

      SetContentView(Resource.Layout.MapView); 
     } 
    } 
} 

avec le fichier de mise en page:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:local="http://schemas.android.com/apk/res-auto" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="@android:color/white"> 
    <RelativeLayout 
     android:layout_width="fill_parent" 
     android:layout_height="0dp" 
     android:layout_weight="0.1" 
     android:background="@drawable/nav_bar_background" 
     android:id="@+id/bartop"> 
    </RelativeLayout> 
    <FrameLayout 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="0.8" 
     android:id="@+id/mapFrame" /> 
    <RelativeLayout 
     android:orientation="horizontal" 
     android:minWidth="25px" 
     android:minHeight="25px" 
     android:layout_width="fill_parent" 
     android:layout_height="0dp" 
     android:layout_weight="0.1" 
     android:id="@+id/barbottom" 
     android:layout_alignParentBottom="true" 
     android:background="@drawable/nav_bar_background"> 
    </RelativeLayout> 
</LinearLayout> 
0

Vous ne pouvez pas gonfler une mise en page dans un fragment quand cette mise en page comprend un fragment . Les fragments imbriqués ne sont pris en charge que lorsqu'ils sont ajoutés dynamiquement à un fragment. Alors ajoutez le fragment par programme.

Questions connexes