SupportMapFragment Map为null

时间:2014-08-25 10:51:26

标签: android google-maps xamarin fragment

我正在使用以下代码在Xamarin.Android中显示地图:

private SupportMapFragment mapFragment;
private GoogleMap map;

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

            SetContentView (Resource.Layout.Main);

            mapFragment = SupportFragmentManager.FindFragmentByTag ("map") as SupportMapFragment;
            if (mapFragment == null) {
                GoogleMapOptions options = new GoogleMapOptions ()
                    .InvokeCompassEnabled (true)
                    .InvokeMapType (GoogleMap.MapTypeNone)
                    .InvokeZoomControlsEnabled (false);

                FragmentTransaction frx = SupportFragmentManager.BeginTransaction ();
                mapFragment = SupportMapFragment.NewInstance (options);
                frx.Add (Resource.Id.map,mapFragment,"map");
                frx.Commit ();
            }


if (map == null)
                map = mapFragment.Map;

            CircleOptions circle = new CircleOptions ();
            circle.InvokeCenter (new LatLng(18.5203,73.8567));
            circle.InvokeRadius (1000);
            map.AddCircle (circle);

我的AXML是

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    class="com.google.android.gms.maps.SupportMapFragment" />

在设备上成功部署后,由于map.AddCircle (circle);System.NullReferenceException投放Object Reference is not set to an instance of an Object,因此我在第{{1}}行上发出例外情况。

我做错了什么?有什么需要初始化的吗?

1 个答案:

答案 0 :(得分:1)

这一行:

SupportFragmentManager.FindFragmentByTag ("map") as SupportMapFragment;

不应该正常运作,因为您试图将View置于Mono.Android之外。因此,您必须使用:

SupportFragmentManager.FindFragmentByTag<SupportMapFragment>("map");

SupportFragmentManager.FindFragmentByTag ("map").JavaCast<SupportMapFragment>();

此外,您正试图通过Tag找到一个片段。但是,您没有在XML中提供Fragment Tag,因此您需要通过Id找到它:

var mapFragment = SupportFragmentManager.FindFragmentById<SupportMapFragment>(Resource.Id.map);

实际上,您已经粘贴了以下代码:

SupportFragmentManager.FindFragmentByTag ("map") as SupportMapFragment;

将永远为null,因为您实际上完全忽略了布局,并且您将始终使用SupportMapFragment条件中手动创建的if (mapFragment == null) ...

另外,我上次检查时,FindFragmentByXXX的绑定不支持泛型类型,因此您可能需要执行JavaCast<T>();

所以,我会修改我的代码看起来更像:

private SupportMapFragment mapFragment;
private GoogleMap map;

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

    SetContentView (Resource.Layout.Main);

    SetUpMapIfNeeded();
}

public override void OnResume()
{
    base.OnResume();
    SetUpMapIfNeeded();
}

private void SetUpMapIfNeeded()
{
    if(null != map) return;

    mapFragment = SupportFragmentManager.FindFragmentById(Resource.Id.map).JavaCast<SupportMapFragment>();
    if (mapFragment != null)
    {
        map = mapFragment.Map;

        if (map == null)
        {
            // throw error here, should never happen though...
            return;
        }

        // set up map here, i.e.:
        map.UiSettings.CompassEnabled = true;
        map.MapType = GoogleMap.MapTypeHybrid;
        map.MyLocationChange += MapOnMyLocationChange;

        CircleOptions circle = new CircleOptions ();
        circle.InvokeCenter (new LatLng(18.5203,73.8567));
        circle.InvokeRadius (1000);
        map.AddCircle (circle);
    }
}