2012-12-04 44 views
-1

上午使用android支持v4谷歌地圖pete doyle我試圖刪除一個mapview然後重新創建它,當我離開並返回它時,但我繼續收到通常的Java.lang.IllegalStateException:您只能在MapActivity中擁有一個MapView。任何人都知道要正確解決這個問題。我在我的片段中有這個代碼。刪除並重新創建一個片段中的mapview android

片段:

@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> 

任何幫助將不勝感激。由於

+0

或者試試onPause()... – logray

+0

@logray你的意思是嘗試初始化onPause()中的MapView? – irobotxxx

+0

不要嘗試在onPause而不是onStop中放置frame.removeView(mapview)或.remooveAllViews。 – logray

回答

2

我用下面的代碼做了這個。我有一個MapView的實例變量在我FragmentActivity,然後加在片段本身下方拆下它(只包含在相關部分):

@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裏。

+0

謝謝!!!!你是一個救星。感謝您抽出寶貴的時間。將不會考慮將我的mapview作爲片段活動中的即時變量。想知道如何......?再次感謝! – irobotxxx

0

嘗試使用frame.removeAllViews()

+0

但我仍然在我初始化mapview的線路上發生錯誤 – irobotxxx

+0

放入一個日誌並檢查是否調用了'onStop()'方法? –

1

通常我這樣做(使用Android的支持-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的方法0

我使用一些設置保存在單獨的xml MapView中。當必須銷燬碎片分離MapView時,在重新創建碎片時連接MapView(您也可以在附加後更新MapView的控制器)。這使我可以重用碎片MapView。

P.S. 我只是沒有找到任何方式清除MapActivity鏈接到MapViev。如果有辦法,我會非常感激。

+0

感謝您的輸入,但我已經有一個FragmentActivity持有片段,所以這是相當困難的,我現在要改變它遲到了。現在我必須在片段中找到解決方案。 – irobotxxx

+0

如果您不需要支持API <8,則可以使用新的GoogleMap服務API及其MapFragment:http://developer.android.com/reference/com/google/android/gms/maps/MapFragment.html – nfirex