0

每次我调用这个函数时,我都会膨胀一个地图视图。并且每次都删除视图。但我仍然收到一个错误,说孩子已经有了父母,请先调用 removeview()。

关于如何解决这个问题的任何想法。

      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);
      }
4

1 回答 1

1

我不确定你在做什么,但最简单的解决方案是添加这一行:

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

到发生错误的地方。我想这发生在这里:

} else {
    ((ViewGroup) mapView.getParent()).removeView(mapView); // Add me
    ((ViewGroup) view.findViewById(R.id.myMap)).addView(mapView);
}
于 2012-11-16T05:35:34.837 回答