【问题标题】:SupportMapFragment Map is nullSupportMapFragment 映射为空
【发布时间】:2014-08-25 10:51:26
【问题描述】:

我正在使用以下代码在 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

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

【问题讨论】:

  • 您确定您使用的布局正确吗?
  • 是的,我使用了正确的布局
  • @user3249477:之前它对我有用。但现在相同的代码给了我例外。
  • 您在 map.AddCircle(circle) 遇到的异常意味着 map == null。在之前的代码中,您正在检查地图上的空值,如果它为空,则执行 map = mapFragment.Map。你不检查那是否不为空。所以很可能 map 和 mapFragment.Map 都是空的。因此这段代码给你这个错误。

标签: android google-maps xamarin fragment


【解决方案1】:

这一行:

SupportFragmentManager.FindFragmentByTag ("map") as SupportMapFragment;

当您尝试在 Mono.Android 之外投射 View 时,应该不起作用。因此你必须使用:

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,因为您实际上完全忽略了布局,并且您将始终在 if (mapFragment == null) 条件中使用手动创建的 SupportMapFragment...

另外,上次我检查时,FindFragmentByXXX 的绑定不支持泛型类型,所以你可能不得不做JavaCast&lt;T&gt;();

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多