Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/security/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 另一个片段中的谷歌地图片段_Android_Android Fragments_Google Maps Android Api 2 - Fatal编程技术网

Android 另一个片段中的谷歌地图片段

Android 另一个片段中的谷歌地图片段,android,android-fragments,google-maps-android-api-2,Android,Android Fragments,Google Maps Android Api 2,我有一个片段显示在选项卡布局中 public class MyFragment extends Fragment { private GoogleMap map; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layou

我有一个片段显示在选项卡布局中

public class MyFragment extends Fragment {

    private GoogleMap map;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_map, container, false);
        map = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
        ...
但是,当再次调用onCreateView时,我遇到了问题,充气方法失败,因为已经存在MapFragment

我不确定这里的正确答案是什么,因为我觉得这是android中的一个bug。也就是说,我没有以编程方式创建SupportMapFragment,所以我觉得不应该以编程方式销毁它

我见过“解决方案”说你需要摧毁它:

public void onDestroy() {
       try {
            Fragment fragment = (getFragmentManager().findFragmentById(R.id.map_fragment));  
            FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
            ft.remove(fragment);
            ft.commit();
        } catch (Exception e) {
            e.printStackTrace();
        }
}
但是,如果我这样做,我会得到:

> 04-07 13:59:00.801: W/System.err(20067):
> java.lang.IllegalStateException: Can not perform this action after
> onSaveInstanceState 04-07 13:59:00.801: W/System.err(20067):  at
> android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1343)
> 04-07 13:59:00.801: W/System.err(20067):  at
> android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1361)
> 04-07 13:59:00.801: W/System.err(20067):  at
> android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:595)
以下是我的XML片段:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:map="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <fragment
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        map:uiCompass="false"
        map:uiRotateGestures="false"
        map:uiTiltGestures="false"/>
</RelativeLayout>

如果Android膨胀了SupportMapFragment,它不应该也破坏该片段。如果不是,我应该如何“正确”销毁SupportMapFragment,以便再次调用onCreateView不会失败,但是,我也不会在onDestroyView中遇到异常。

我尝试使用supportMapFragment或带有不同类型片段的MapFragment,但它总是给我带来困难,因此对我来说唯一有效的解决方案是手动实现它,实际上只需要几行代码

这是地图活动

public class Map extends  SherlockFragment implements OnMapClickListener {

    public static GoogleMap googleMap;
private MapView mMapView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.map);   
     mMapView = (MapView) findViewById(R.id.mapview);
        mMapView.onCreate(savedInstanceState);
        initilizeMap();

    }



    /**
     * function to load map. If map is not created it will create it for you
     * */

    private void initilizeMap() {
        googleMap = null;
            googleMap = ((MapView) findViewById(R.id.mapview)).getMap();

            if (googleMap != null) {
              googleMap.setMapType(googleMap.MAP_TYPE_NORMAL);
              googleMap.setOnMapClickListener(this);
              googleMap.setMyLocationEnabled(true);
                googleMap.setLocationSource(new CurrentLocationProvider(this));  

            }



    }



      @Override
        public void onResume() {
            super.onResume();
            mMapView.onResume();
            Log.e("onResume","onResume");

        }


        @Override
        public void onPause() {
            super.onPause();
            mMapView.onPause();
            Log.e("onPause","onPause");

        }

        @Override
        public void onDestroy() {
            if(mMapView!=null){
            mMapView.onDestroy();
            Log.e("map view not null !!","it is not null");

            }else{
                Log.e("map view null!!","it is null");
            }
            super.onDestroy();

        }
    @Override
    public void onMapClick(LatLng latng) {

    }



}
CurrentLocationProvider:

public class CurrentLocationProvider implements LocationSource, LocationListener
{
    private OnLocationChangedListener listener;
    private LocationManager locationManager;

    public CurrentLocationProvider(Context context)
    {
        locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
    }

    @Override
    public void activate(OnLocationChangedListener listener)
    {
        this.listener = listener;
        LocationProvider gpsProvider = locationManager.getProvider(LocationManager.GPS_PROVIDER);
        if(gpsProvider != null)
        {
            locationManager.requestLocationUpdates(gpsProvider.getName(), 0, 10, this);
        }

        LocationProvider networkProvider = locationManager.getProvider(LocationManager.NETWORK_PROVIDER);;
        if(networkProvider != null) {
            locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000 * 60 * 5, 0, this);
        }
    }

    @Override
    public void deactivate()
    {
        locationManager.removeUpdates(this);
    }

    @Override
    public void onLocationChanged(Location location)
    {
        if(listener != null)
        {
            listener.onLocationChanged(location);
        }
    }

    @Override
    public void onProviderDisabled(String provider)
    {
        // TODO Auto-generated method stub

    }

    @Override
    public void onProviderEnabled(String provider)
    {
        // TODO Auto-generated method stub

    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras)
    {
        // TODO Auto-generated method stub

    }
}
布局如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >


    <com.google.android.gms.maps.MapView
        android:id="@+id/mapview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="0.75" />



</LinearLayout>

这是使用旧的MapView,而不是使用MapFragment。