0

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()

user1336543
  • 185
  • 1
  • 6
  • Did you override onBackPressed? If no, can you post what you are doing in any of the following methods: onPause, onStop, onDestroy? – WindyB Jul 07 '12 at 16:59
  • I didn't override any other method except onPause, and I've added that above. Just setting things null and clearing views, ...aggressive memory reclaiming... – user1336543 Jul 07 '12 at 19:37
  • Do you have a MapView being used in your previous Activity? I would try removing the MapView entirely and seeing if the IllegalStateException still persists. If it doesn't, then you can go line-by-line through the lines that touch your MapView to see what's causing the problem. – WindyB Jul 08 '12 at 16:33
  • I don't create mapviews anywhere else in the code: I only create mapviews in the above example. What is causing the problem is the new MapView line when I hit the backbutton (from the next activity) to return to THIS activity. – user1336543 Jul 08 '12 at 16:43

1 Answers1

0

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.

Community
  • 1
  • 1
WindyB
  • 786
  • 3
  • 9
  • Thanks WindyB, I had stumbled upon this answer as well through chance! I guess I'll have to trust that the Garbage Collector grabs MapView when it takes out MapActivity? – user1336543 Jul 10 '12 at 14:58