android – 您只能在MapActivity中拥有一个MapView
作者:互联网
我有一个显示地图页面的功能,所以我可以让用户选择他们当前的位置.但是如果你运行这个函数两次,它会在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);
}
所以我有一个显示此View和onClick =“showMapSetterPage”的按钮.但是,如果您从地图中返回设置屏幕并再次单击该按钮,则会收到以下错误:
03-06 20:55:54.091:
ERROR/AndroidRuntime(28014): Caused
by: java.lang.IllegalStateException:
You are only allowed to have a single
MapView in a MapActivity
如何删除MapView并重新创建它?
解决方法:
我认为每个人都是对的,它对我来说就像是一个缺陷.我应该能够给视图充气并在其中使用地图,如果我回想起视图,那么就可以删除它或者再次查看它而不会出错.
最简单的解决方法是使用xml删除膨胀或setContentView,并使用动态构建的映射,然后将其存储在内存中.
我删除了:
// Show Map Settings Screen
setContentView(R.layout.set_map_center);
// Initiate the center point map
if (mapView == null) {
mapView = (MapView) findViewById(R.id.mapview);
}
并替换为:
if (mapView == null) {
// public MapView mapView = null; // Public defined Variable
mapView = new MapView(this, this.getString(R.string.APIMapKey));
}
setContentView(mapView);
这很好用,让我有机会打电话给地图.感谢您回复所有人.
标签:android-maps,android 来源: https://codeday.me/bug/20190927/1824713.html