【问题标题】:SupportMapFragment causes exception on orientation changeSupportMapFragment 导致方向更改异常
【发布时间】:2013-08-02 21:38:15
【问题描述】:

以下代码在单一方向上运行良好,但一旦方向改变就会导致异常:

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

如果我在 OnCreate 中注释掉 InitMapFragment,那么地图会正确加载和旋转,所以它似乎与 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.

【问题讨论】:

  • 有什么异常?
  • OnCreate 上的 InflateException。我在问题中添加了额外的细节。

标签: xamarin.android mvvmcross supportmapfragment


【解决方案1】:

我仍然不确定错误的原因,但是将片段更改为 XML 中的 FrameLayout,然后以编程方式添加片段修复它。

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>

【讨论】:

    【解决方案2】:

    当布局包含片段时,您不能将布局扩展为片段。仅当动态添加到片段时才支持嵌套片段。所以以编程方式添加片段。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-10-27
      • 2013-02-12
      • 2023-03-04
      • 2012-11-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多