MapActivity 中只能有一个 MapView
我有一个显示地图页面的功能,这样我就可以让用户选择他们当前的位置。但是,如果您运行此函数两次,它会导致应用程序在 MapActivity 错误中使用单个 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: 错误/AndroidRuntime(28014):引起 通过:java.lang.IllegalStateException: 您只能拥有一个 MapActivity 中的 MapView
如何删除 MapView 并重新创建它?
I have a function that shows a map page so I can get the user to choose their current location. But if you run this function twice it crashes the App with the Single MapView in a MapActivity error (i.e. opening that settings view again).
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);
}
So I have a button that shows this View and onClick="showMapSetterPage". But if you go back to the settings screen out of the map and click the button again I get this error:
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
How can I delete the MapView and recreate it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为每个人都有点对,对我来说这似乎是 API 中的一个缺陷。我应该能够膨胀视图并使用其中的地图,如果我回忆起该视图,则能够删除它或再次查看它而不会出现错误。
最简单的解决方法是使用 xml 删除通货膨胀或 setContentView 并动态构建地图,然后将其存储在内存中。
我删除了:
并将其替换为:
这非常有效,让我有机会调用地图。感谢大家的回复。
I think everyone was a little right, it seams like a flaw in the API to me. I should be able to inflate a view and use the map in it, if I recall the view then be able to delete it or review it again without error.
The easiest workaround was to remove the inflation or setContentView using the xml and going with a dynamic build of the map, then storing that in memory.
I removed:
And replaced it with:
This works great and gives me chances to call the map. Thanks for responding everyone.
因为您多次调用
setContentView(R.layout.set_map_center);
。为什么这里还要重新设置contentView呢?为什么不把它放在
onCreate
中呢?Because you call
setContentView(R.layout.set_map_center);
more than once.Why do you need to set the contentView here again? Why not put it in
onCreate
?这里还有另一个对我有用的解决方案:
FragmentMap + ActionBar Tab
Here there is another solution that worked for me:
FragmentMap + ActionBar Tab