这行:
SupportFragmentManager.FindFragmentByTag ("map") as SupportMapFragment;
当您尝试投射时不应该起作用View
来到外面Mono.Android
。因此你必须使用:
SupportFragmentManager.FindFragmentByTag<SupportMapFragment>("map");
or
SupportFragmentManager.FindFragmentByTag ("map").JavaCast<SupportMapFragment>();
您还试图通过以下方式找到片段Tag
。然而,你并没有给出你的Fragment
a Tag
在你的XML中,所以你需要通过以下方式找到它Id
反而:
var mapFragment = SupportFragmentManager.FindFragmentById<SupportMapFragment>(Resource.Id.map);
实际上,从您粘贴的代码中可以看出:
SupportFragmentManager.FindFragmentByTag ("map") as SupportMapFragment;
将始终为空,因为您实际上完全忽略了布局,并且您将始终使用手动创建的SupportMapFragment
in the 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);
}
}