我有一个显示地图页面的功能,因此我可以让用户选择他们当前的位置。但是,如果您两次运行此功能,它会在 MapActivity 错误中使用 Single MapView 使应用程序崩溃(即再次打开该设置视图)。
public void showMapSetterPage(View v) {
Log.i(DEBUG_TAG, "Settings screen, set map center launched");
// Set which view object we fired off from
set_pv(v);
// Show Map Settings Screen
setContentView(R.layout.set_map_center);
// Initiate the center point map
if (mapView == null) {
mapView = (MapView) findViewById(R.id.mapview);
}
mapView.setLongClickable(true);
mapView.setStreetView(true);
mapView.setBuiltInZoomControls(false);
mapView.setSatellite(false);
mapController = mapView.getController();
mapController.setZoom(18);
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Location location = lm
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
int lat = (int) (location.getLatitude() * 1E6);
int lng = (int) (location.getLongitude() * 1E6);
Log.i(DEBUG_TAG, "The LAT and LONG is: " + lat + " == " + lng);
point = new GeoPoint(lat, lng);
// mapController.setCenter(point);
mapController.animateTo(point);
}
所以我有一个按钮显示这个视图和 onClick="showMapSetterPage"。但是,如果您返回地图外的设置屏幕并再次单击按钮,我会收到此错误:
03-06 20:55:54.091: ERROR/AndroidRuntime(28014): Caused by: java.lang.IllegalStateException: 你只能在 MapActivity 中拥有一个 MapView
如何删除 MapView 并重新创建它?