Search code examples
androidandroid-mapviewmapactivity

MapView / MapActivity crashes onBackPressed by Child Activity


I am trying to create an informational Activity, which displays a location on google's MapView among other things. Whenever I navigate through this MapActivity, then click the back button, I get the following error:

"... Caused by: java.lang.IllegalStateException: You are only allowed to have a single MapView in a MapActivity..."

Things you should know! :

onResume calls my setUpDisplay which initializes my MapView in a LinearLayout:

...

        List<Address> geoResults = geocoder.getFromLocationName(formalAddress, 1);
    while (geoResults.size()==0) 
    {
        geoResults = geocoder.getFromLocationName("empire state building", 1);
    }
    if (geoResults.size()>0) 
    {
        point= new GeoPoint( (int) (geoResults.get(0).getLatitude() * 1E6), (int) (geoResults.get(0).getLongitude() * 1E6));
    }
    mapView = new MapView(this, "YOU-NO-GET-TO-SEE-MY-KEY-HERE!");


    MapController mapController = mapView.getController();

    mapController.animateTo(point);
    mapController.setZoom(15);


               myMainLinearLayout.addView(mapView);

...

I also make sure to call myMainLinearLayout.removeAllViews() on my overrided onPause(), as well as setting variables to null, just to make sure all things are getting cleaned up.

I have also tried to see if, before the mapView initialization, mapView == null -- It Never Is. Anyone have any ideas?

Thanks in advance. -Ethan

--------EDIT:

Here is what I'm doing in my onPause method, in my MapActivity

@Override
public void onPause()
{//CLEANUP
    super.onPause();
    myMainLinearLayout.removeAllViews();
    myMainLinearLayout= null;
    mapView = null;
}

that's the only method I am overriding besides onResume()


Solution

  • This post should help:

    You are only allowed to have a single MapView in a MapActivity

    Summary: Do not destroy your previous MapView. Instantiate it once in onCreate, and modify it whenever you need to directly.