我使用android兼容类和hack,在这里找到的片段中使用mappviews:https://github.com/petedoyle/android-support-v4-googlemaps
不幸的是,我发现如果mappfragment从活动中删除,然后重新读取,我会得到“您只能在mappactivity中有一个mappview”错误。
我理解错误背后的原理,并尝试在fragments onpause方法中销毁mappview。不幸的是,我似乎不能完全破坏地图视图,因为我仍然得到它。我的代码如下:

private RelativeLayout layout;
private MapView mp;

public void onResume(){
    super.onResume();
    Bundle args = getArguments();
    if(mp == null)
    {
        mp = new MapView(getActivity(), this.getString(R.string.map_api_key));
        mp.setClickable(true);
    }

    String request = args.getString("requestId");
    layout = (RelativeLayout) getView().findViewById(R.id.mapholder);
    layout.addView(mp);
    //TextView txt = (TextView) getView().findViewById(R.id.arguments);
    //txt.setText(request);
}

public void onPause(){
    super.onPause();
    layout.removeView(mp);
    mp = null;
}

有没有人对我忽略销毁的参考文献有什么想法?

最佳答案

我遇到了同样的问题。我是这样解决的:
因为它应该只是活动中mappview的一个实例,所以我在活动中的oncreate方法中初始化它:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // initialize MapView programmatically to be used in Fragment :
    this.mActivityMapView = new MapView(MainActivity.this, getString(R.string.debug_mapview_apikey));

    setContentView(R.layout.activity_main);
}

然后在fragment oncreateview方法中恢复它:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    this.mMapView = ((MainActivity) getActivity()).getMapView();
    return this.mMapView;
}

我用碎片解救法摧毁它:
public void onDestroy() {
    NoSaveStateFrameLayout parentView = (NoSaveStateFrameLayout) this.mMapView.getParent();
    parentView.removeView(this.mMapView);
    super.onDestroy();
}

08-19 08:34