我正在使用Android兼容性类与黑客在这里找到的片段中使用mapview:https://github.com/petedoyle/android-support-v4-googlemaps
不幸的是,我发现如果mapfragment从活动中删除,然后重新读取,我会得到“你只允许在MapActivity中有一个MapView”错误。“
我理解错误背后的原理,并尝试在片段onPause方法中销毁mapview。不幸的是,我似乎无法完全破坏mapview,因为我仍然得到它。我的代码看起来像这样:
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;
}
有没有人对我在这里忽略的引用有什么想法?
答案 0 :(得分:15)
我遇到了同样的问题。以下是我解决它的方法:
因为它应该只是活动中mapView的一个实例,我在Activity中的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);
}
然后我在片段onCreateView方法中恢复它:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
this.mMapView = ((MainActivity) getActivity()).getMapView();
return this.mMapView;
}
我在片段onDestroy方法中将其销毁:
public void onDestroy() {
NoSaveStateFrameLayout parentView = (NoSaveStateFrameLayout) this.mMapView.getParent();
parentView.removeView(this.mMapView);
super.onDestroy();
}
答案 1 :(得分:6)
您可能想要删除onPause方法中的地图视图(而不是onDestroy方法)
public void onPause() {
NoSaveStateFrameLayout parentView = (NoSaveStateFrameLayout) this.mMapView.getParent();
parentView.removeView(this.mMapView);
super.onPause();}
这样,您可以将MapFragment添加到Backstack。 (FragmentTransaction.addToBackStack可防止碎片被破坏)
答案 2 :(得分:6)
Max和Bleeker的答案效果很好但是如果你想支持屏幕方向的改变,你需要特别小心,就像我一样。当你旋转屏幕时,它会再次调用OnCreateView而不调用onPause方法,因此添加了两次mapview。要修复它,在OnCreateView中我执行了以下操作:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
this.mMapView = ((MyGameActivity) getActivity()).getMapView();
if (this.mMapView.getParent()!=null)
{
NoSaveStateFrameLayout parentView = (NoSaveStateFrameLayout) this.mMapView.getParent();
parentView.removeView(this.mMapView);
}
return this.mMapView;
}
答案 3 :(得分:4)
我试图找到解决这个问题的简单方法。
在处理我的View详细信息(RelativeLayout扩展)的类中,我声明了private static MapView myMapView;
。
然后,覆盖onFinishInflate()方法,如下面的代码片段:
@Override
protected void onFinishInflate() {
super.onFinishInflate();
//myMapView = (MapView) findViewById(R.id.map_view);
if (myMapView==null)
myMapView = new MapView(context, getResources().getString(R.string.map_view_api_key));
if (myMapView.getParent() != null)
((ViewGroup)myMapView.getParent()).removeView(myMapView);
myMapView.setClickable(true);
myMapView.setEnabled(true);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
myMapView.setLayoutParams(lp);
this.addView(myMapView);
}
这样你只有一个MapView实例。
答案 4 :(得分:0)
以下是我在片段中解决此问题的方法。
创建布局
<LinearLayout
android:id="@+id/ll_mapcontainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" />
您可以将此布局放置在XML文件中要显示地图的任何位置
现在代码就像这样。
package com.yuviii.sample;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.yuviii.sample.R;
/**
* Created by yubraj Poudel on 12/20/15.
*/
public class ContactUsFragment extends Fragment {
static final LatLng PRACTICAL_ANSWER_LOCATION = new LatLng(53.558, 9.927);
private SupportMapFragment fragment;
private GoogleMap map;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.page_contactus, container, false);
return v;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
initializeMap();
}
private void initializeMap() {
FragmentManager fm = getChildFragmentManager();
fragment = (SupportMapFragment) fm.findFragmentById(R.id.ll_mapcontainer);
if (fragment == null) {
fragment = SupportMapFragment.newInstance();
fm.beginTransaction().replace(R.id.ll_mapcontainer, fragment).commit();
}
}
@Override
public void onResume() {
super.onResume();
if (map == null) {
map = fragment.getMap();
map.addMarker(new MarkerOptions().position(PRACTICAL_ANSWER_LOCATION));
}
}
}
这对我有用。享受!!!