删除并重新创建片段android中的mapview

时间:2012-12-04 17:41:02

标签: android

我正在使用 android支持v4-谷歌地图由pete doyle ,我正在尝试删除mapview,然后重新创建它,当我离开并返回它,但我一直得到通常 Java.lang.IllegalStateException:您只能在MapActivity中拥有一个MapView 。任何人都知道要正确解决这个问题。我的片段中有这个代码。

片段:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    super.onCreateView(inflater, container, savedInstanceState);
    Log.d(TAG, "onCreate View called here in location fragment!!");

    if(view == null){
      view = inflater.inflate(R.layout.map, container, false);  

    frame = (FrameLayout)view.findViewById(R.id.mapContainer);
    //mapview = (MapView)view.findViewById(R.id.mapView);
    }

    if(mapview == null){

        mapview = new MapView(getActivity(), getActivity().getString(R.string.map_api_key));  // keep getting the error on this line
        mapview_is_active = true;
    }

    mapview.setClickable(true);
    mapview.setBuiltInZoomControls(true);
    mapview.setSatellite(true); // Satellite View
    mapview.setStreetView(true); // Street View
    mapview.setTraffic(true); // Traffic View

    frame.addView(mapview,0);



    //frame = (FrameLayout)view.findViewById(R.id.mapContainer);   
   return view;
}


  @Override
    public void onActivityCreated(Bundle savedInstanceState){
        super.onActivityCreated(savedInstanceState);

       if(mapview == null){
           Log.d(TAG, "mapview is null here");
           mapview = new MapView(getActivity(), getActivity().getString(R.string.map_api_key));
       }

       if(frame.getChildAt(0) == null){
           Log.d(TAG, "mapview is null here in container");
           frame.addView(mapview);
       }

        MapController controller = mapview.getController();

        double lat = Double.parseDouble(longitude);
        double lon = Double.parseDouble(latitude);

        GeoPoint geopoint = new GeoPoint((int)(lat * 1E6), (int)(lon * 1E6));

        mapview.invalidate();

        List<Overlay> mapOverlays = mapview.getOverlays();
        Drawable drawable = this.getResources().getDrawable(R.drawable.map_point);
        AddMapOverlay add_map_overlay = new AddMapOverlay(drawable, getActivity());

        OverlayItem overlayitem = new OverlayItem(geopoint, name, address);

        add_map_overlay.addOverlay(overlayitem);

        mapOverlays.add(add_map_overlay);

        controller.animateTo(geopoint);
        controller.setZoom(15);
    }

和我的onStop():

 @Override
    public void onStop(){
        super.onStop(); 

        if(frame != null){
            frame.removeView(mapview); // i remove the mapview here
        }   
    }

这是我的map.xml:

<FrameLayout  xmlns:android="http://schemas.android.com/apk/res/android"
   android:id="@+id/mapContainer"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:layout_weight="1">

</FrameLayout>

任何帮助将不胜感激。感谢

3 个答案:

答案 0 :(得分:2)

我用下面的代码做了这个。我在FragmentActivity中有一个MapView实例变量,然后在下面的Fragment中添加和删除它(仅包括相关部分):

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    mLayoutMap = (FrameLayout) getActivity().findViewById(R.id.layoutMap);

    // create the map view if it's not already there
    if (((MyActivity)getActivity()).mMapView == null)
    {
        ((MyActivity)getActivity()).mMapView = 
                    new MapView(getActivity(), getActivity().getString(R.string.map_api_key));
        ((MyActivity)getActivity()).mMapView.setClickable(true);
    }

    // add the map view to the frame layout, at the lowest z-index
    mLayoutMap.addView(((MyActivity)getActivity()).mMapView, 0);

}

@Override
 public void onStop()
 {
     super.onStop();

     if (mLayoutMap.getChildCount() > 0 && mLayoutMap.getChildAt(0) instanceof MapView)
         mLayoutMap.removeViewAt(0);
 }

mLayoutMap只是我XML中的FrameLayout。

答案 1 :(得分:1)

通常我这样做(使用android-support-v4-google_maps):

1)使用attachMap和detachMap

方法实现IMapActivity等活动界面
public class MyMapActivity extends FragmentActivity implements IMapActivity {
    private MapView mMapView;

    public void attachMap(ViewGroup mapHolder) {
        if (mMapView == null) {
            mMapView = getLayoutInflater().inflate(R.id.mapview, null);
        }

        mapHolder.addView(mMapView);
    }

    public void detachMap(ViewGroup mapHolder) {
        mapHolder.removeView(mMapView);
    }
}

2)在需要附加MapView或在Fragment.onDestroyView()上分离MapView时调用该方法

public class MyMapFragment extends Fragment {
    private IMapActivity mMapViewHolder;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        mMapViewHolder = (IMapActivity) activity;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mMapViewHolder.attachMap(getMapHolder());
    }

    @Override
    public void onDestroyView() {
        mMapViewHolder.detachMap(getMapHolder());
        super.onDestroyView();
    }
}

我使用一些设置保存在单独的xml MapView中。当需要销毁Fragment - 分离MapView时,在重新创建Fragment时附加MapView(您也可以在附加后更新MapView的控制器)。这允许我重用片段MapView。

<强> P.S。 我还没有找到任何方法如何清理MapActivity链接到MapViev。如果有办法,我将非常感激。

答案 2 :(得分:0)

尝试使用frame.removeAllViews()