android support v4- google maps by pete doyleを使用しており、マップビューを削除してから、マップビューから離れて戻ったときに再度作成しようとしていますが、通常の Java.lang.IllegalStateException: You are only 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>
どんな助けでも大歓迎です。ありがとう