0
我在本網站上關注了本教程:http://www.c-sharpcorner.com/article/xamarin-android-create-google-map-with-marker/。它在MainActivity中100%工作,但只要我將代碼移動到片段中,我就會收到一條錯誤消息,說'對象未設置爲實例'。可能是什麼問題呢 ?這裏是我的代碼:如何在片段中使用FragmentManager - Xamarin
public class ViewAlert : Fragment, IOnMapReadyCallback
{
View view = null;
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
view = inflater.Inflate(Resource.Layout.ViewAlert, container, false);
try
{
SetUpMap();
}
catch (Exception ex)
{
}
return view;
}
private void SetUpMap()
{
if (GMap == null)
{
FragmentManager.FindFragmentById<MapFragment>(Resource.Id.googlemap).GetMapAsync(this);
}
}
public void OnMapReady(GoogleMap googleMap)
{
this.GMap = googleMap;
GMap.UiSettings.ZoomControlsEnabled = true;
LatLng latlng = new LatLng(Convert.ToDouble(gpsLatitude), Convert.ToDouble(gpsLongitude));
CameraUpdate camera = CameraUpdateFactory.NewLatLngZoom(latlng, 15);
GMap.MoveCamera(camera);
MarkerOptions options = new MarkerOptions()
.SetPosition(latlng)
.SetTitle("Chennai");
GMap.AddMarker(options);
}
}
在這條線上出現的錯誤:
FragmentManager.FindFragmentById<MapFragment>(Resource.Id.googlemap).GetMapAsync(this);
我想:
view.FragmentManager.FindFragmentById<MapFragment>(Resource.Id.googlemap).GetMapAsync(this);
但沒有奏效。
地圖加載時沒有標記。破壞代碼負責將標記放置在地圖上。
它的工作完全正常。謝謝 –