Delete view and recreate it

Is there a way to remove the view that was installed with

setContentView(R.layout.set_map_center); mapView = (MapView) findViewById(R.id.mapview); 

If I call this view again, I get an error:

java.lang.IllegalStateException: You are allowed to have only one MapView in MapActivity

+4
source share
3 answers

I think everyone was a little right, it seems like an error in the API for me. I should be able to inflate the view and use the map in it, if I remember the view, then delete it or view it again without errors.

The simplest workaround was to remove inflation or setContentView using xml and go with the dynamic assembly of the map and then save it in memory.

I deleted:

 // 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 the ability to call a card. Thank you for answering everyone.

0
source
 ((ViewGroup)mapView.getParent()).removeView(mapView); 
+1
source

Try it. first inflate the view you are trying to set in setContentView () with the 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 a view, you can just say layout.setVisibility (GONE) if you do not have GONE access, say View.GONE

0
source

Source: https://habr.com/ru/post/1342586/


All Articles