2013-08-02 53 views
0

下面的代碼工作正常,在一個單一的方向,但只要方向改變導致異常:SupportMapFragment引起的方向變化異常

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(); 
      } 
     } 
    } 
} 

如果我註釋掉InitMapFragment在OnCreate中那麼地圖加載和正確旋轉,所以它看起來與SupportMapFragment有關。

佈局文件包含:

<?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> 

調試輸出只顯示 '未處理的異常發生。'但該設備日誌顯示:

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

什麼是例外? – Cheesebaron

+0

OnCreate上的InflateException。我爲這個問題增加了額外的細節。 – Neil

回答

1

我仍然不知道錯誤的原因,但改變片段到的FrameLayout的XML,然後加入片段編程修復它。

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); 
     } 
    } 
} 

與佈局文件:

<?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

不能膨脹佈局成片段時佈局包括片段。只有在動態添加到片段時才支持嵌套片段。因此以編程方式添加片段。