删除并重新创建一个片段Android上的MapView [英] remove and recreate a mapview in a fragment android
问题描述
现在用的 Android的支持V4-谷歌皮特·道尔的地图并正在试图删除一个图形页面,然后再重新创建它,当我离开并返回到它,但我不断收到通常的 Java.lang.IllegalStateException:您只允许有一个图形页面中MapActivity 。任何人都知道豪正确地解决这个问题。我在我的这个片段code。
am using the android support v4- google maps by pete doyle and am trying to delete a mapview and then recreate it again, when i go away from and return to it, but i keep getting the usual Java.lang.IllegalStateException: You are only allowed to have a single MapView in a MapActivity. anyone know ho to solve this correctly. I have this code in my fragment.
片段:
@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():
and in my onStop():
@Override
public void onStop(){
super.onStop();
if(frame != null){
frame.removeView(mapview); // i remove the mapview here
}
}
这是我的map.xml:
here is my 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>
任何帮助将大大AP preciated。谢谢
any help will be greatly appreciated. Thanks
推荐答案
我这样做是在低于code。我有一个图形页面实例变量在我FragmentActivity,然后加入并在片段本身下方(只是包含在相关部分)中除去它
I did this with the below code. I have a MapView instance variable in my FragmentActivity, and then added and removed it in the Fragment itself below (just included the pertinent parts):
@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只是在的FrameLayout我的XML。
mLayoutMap is just a FrameLayout in my XML.
这篇关于删除并重新创建一个片段Android上的MapView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!