SupportMapFragment地图为空 [英] SupportMapFragment Map is null

查看:193
本文介绍了SupportMapFragment地图为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用下面的code以显示Xamarin.Android一个图:

I am using following code to display a map in 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(圈子)异常; System.NullReferenceException 已引发对象引用不设置到对象的实例

我是什么做错了吗?有没有需要初始化的东西?

What am I doing wrong? Is there something needed to be initialized?

推荐答案

这行:

SupportFragmentManager.FindFragmentByTag ("map") as SupportMapFragment;

如果无法正常工作,您要投 Mono.Android 查看到来之外。因此,你必须使用:

Should not work as you are trying to cast a View coming outside of Mono.Android. Hence you must use:

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

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

另外你正在努力寻找一个片段通过标签。但是,你没有给你的片段 A 标签在你的XML,所以你需要通过<$找到它C $ C>编号来代替:

Also you are trying to find a fragment by Tag. However, you didn't give your Fragment a Tag in your XML, so you need to find it by Id instead:

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

其实从code已粘贴的行:

Actually from the code you have pasted the line:

SupportFragmentManager.FindFragmentByTag ("map") as SupportMapFragment;

将始终为空,因为你实际上是忽略了布局完全消失,而你总是会使用手动创建 SupportMapFragment 如果(mapFragment == NULL)条件...

另外,我最后一次检查,绑定为 FindFragmentByXXX 不支持泛型类型,所以你可能会做 JavaCast&LT; T&GT;();

Also, last time I checked, the bindings for the FindFragmentByXXX didn't support the generic type, so you will probably have to do the JavaCast<T>();

所以,我将修改我的code看起来更像是:

So, I would revise my code to look more like:

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

这篇关于SupportMapFragment地图为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆