Delete a view and recreate it
Is the开发者_JS百科re a way to remove a view that was set with
setContentView(R.layout.set_map_center);
mapView = (MapView) findViewById(R.id.mapview);
If I call this view again then I get an error saying:
java.lang.IllegalStateException: You are only allowed to have a single MapView in a MapActivity
((ViewGroup)mapView.getParent()).removeView(mapView);
Try this.. first inflate the view which you are trying to set in setContentView() by following code;
layout = new LinearLayout(context);
layout.setVisibility(VISIBLE);
v = View.inflate(getContext(), R.layout.set_map_center, layout);
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.NO_GRAVITY;
addView(layout, params);
v.setVisibility(VISIBLE);
layout.setVisibility(VISIBLE);
whenever you want to delete the view you can simply say layout.setVisibility(GONE) if you are not able access GONE say View.GONE
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:
// Show Map Settings Screen
setContentView(R.layout.set_map_center);
// Initiate the center point map
if (mapView == null) {
mapView = (MapView) findViewById(R.id.mapview);
}
And replaced it with:
if (mapView == null) {
// public MapView mapView = null; // Public defined Variable
mapView = new MapView(this, this.getString(R.string.APIMapKey));
}
setContentView(mapView);
This works great and gives me chances to call the map. Thanks for responding everyone.
精彩评论