zoram
zoram

Reputation: 512

Inflating Map View repeatedly

I am inflatng a mapview every time i call this function. And am also removing the view each time . but still i get an error saying the child already has a parent ,call removeview() first.

Any idea of how to solve this .

      void getGeoView(Context context) {
        myRoot.removeAllViews();

       LayoutInflater inflater = (LayoutInflater) this
            .getSystemService(LAYOUT_INFLATER_SERVICE);
      View view = inflater.inflate(R.layout.geo_inflator, null);

       ((ViewGroup) view.findViewById(R.id.myMap)).removeAllViews();

      if (mapView == null) {
        mapView = new MapView(this, "0IEmqsWhwmo6Cu8hKdYn_VudUT8IpdKpXzMvQyw");
        ((ViewGroup) view.findViewById(R.id.myMap)).addView(mapView);
      } else {

        ((ViewGroup) view.findViewById(R.id.myMap)).addView(mapView);
      }

Upvotes: 0

Views: 125

Answers (1)

Sam
Sam

Reputation: 86948

I'm not sure what you are doing but the simplest solution is to add this line:

((ViewGroup) mapView.getParent()).removeView(mapView);

to wherever the error occurs. I guess that happens here:

} else {
    ((ViewGroup) mapView.getParent()).removeView(mapView); // Add me
    ((ViewGroup) view.findViewById(R.id.myMap)).addView(mapView);
}

Upvotes: 1

Related Questions