You are allowed to have only one MapView in MapActivity

I have a function that displays a map page, so I can get the user to choose their current location. But if you run this function twice, it resets the application using the Single MapView in the MapActivity error (i.e., it opens this 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 displays this view and onClick = "showMapSetterPage". But if you return to the settings screen from the map and press the button again, I will get this error:

03-06 20: 55: 54.091: ERROR / AndroidRuntime (28014): called by: java.lang.IllegalStateException: You are allowed to have only one MapView in MapActivity

How can I remove MapView and recreate it?

+1
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.

+3
source

Because you are calling setContentView(R.layout.set_map_center); more than once. Why do you need to install ContentView again? Why not put it in onCreate ?

0
source

Here is another solution that worked for me:

FragmentMap + ActionBar tab

0
source

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


All Articles