Android SupportMapFragment映射为空
我使用以下代码在Xamarin.Android中显示地图:Android SupportMapFragment映射为空,android,google-maps,xamarin,fragment,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);
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
被抛出对象引用未设置为对象的实例
我做错了什么?是否需要初始化某些内容?此行:
SupportFragmentManager.FindFragmentByTag ("map") as SupportMapFragment;
当您试图在Mono.Android
之外投射视图时,不应工作。因此,您必须使用:
SupportFragmentManager.FindFragmentByTag<SupportMapFragment>("map");
实际上,根据您粘贴的代码行:
SupportFragmentManager.FindFragmentByTag ("map") as SupportMapFragment;
将始终为null,因为您实际上完全忽略布局,并且您将始终在if(mapFragment==null)
条件中使用手动创建的SupportMapFragment
另外,上次我检查时,FindFragmentByXXX
的绑定不支持泛型类型,因此您可能必须执行JavaCast()代码>
因此,我将修改代码,使其看起来更像:
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);
}
}
private-SupportMapFragment-mapFragment;
私人谷歌地图;
创建时受保护的覆盖无效(捆绑包)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
setupmapifneed();
}
公共覆盖void OnResume()
{
base.OnResume();
setupmapifneed();
}
私有void SetUpMapIfNeeded()
{
if(null!=map)返回;
mapFragment=SupportFragmentManager.FindFragmentById(Resource.Id.map).JavaCast();
if(mapFragment!=null)
{
map=mapFragment.map;
if(map==null)
{
//在这里抛出错误,但永远不会发生。。。
返回;
}
//在此处设置地图,即:
map.UiSettings.CompassEnabled=true;
map.MapType=GoogleMap.MapTypeHybrid;
map.MyLocationChange+=MapOnMyLocationChange;
CircleOptions循环=新的CircleOptions();
circle.InvokeCenter(新车床(18.5203,73.8567));
调用圆(1000);
map.AddCircle(圆);
}
}
您确定使用了正确的布局吗?是的,我使用的是正确的布局layout@user3249477字体早些时候它对我有用。但现在同样的代码给了我一个异常。在map.AddCircle(circle)中得到的异常意味着map==null。在之前的代码中,您正在检查map上的null,如果为null,则执行map=mapFragment.map。但是,您不会检查它是否为null。所以很可能map和mapFragment.map都是空的。因此,这段代码给出了这个错误。
SupportFragmentManager.FindFragmentByTag ("map") as SupportMapFragment;
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);
}
}