onCreateView()中导航抽屉中的Android Google Map v2

时间:2014-08-14 19:53:04

标签: android google-maps android-fragments

我的Android应用程序出现问题。我在PlaceholderFragment中使用导航抽屉和onCreateView(),如下所示:

// Google Map
private GoogleMap map;
private final LatLng LOCATION_DEPAUW = new LatLng(39.640343, -86.860687);

public static class PlaceholderFragment extends Fragment {

/**
     * Returns a new instance of this fragment for the given section number.
     */
    public static PlaceholderFragment newInstance(int sectionNumber) {
        PlaceholderFragment fragment = new PlaceholderFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_SECTION_NUMBER, sectionNumber);
        fragment.setArguments(args);
        return fragment;
    }

    public PlaceholderFragment() {
    }

    @SuppressWarnings("unused")
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View rootView = null;

...

else if(mTitle.equals("Map")){
            Log.d("Map", "Worked");
            rootView = inflater.inflate(R.layout.google_map, container, false);

            try {                       

                    if (map == null) {
                        SupportMapFragment fm = (SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.map);
                        map = fm.getMap();
                    }


                    // Needs to call MapsInitializer before doing any CameraUpdateFactory calls
                    try {
                        MapsInitializer.initialize(this.getActivity());
                    } catch (GooglePlayServicesNotAvailableException e) {
                        e.printStackTrace();
                    }

                    CameraUpdate update = CameraUpdateFactory.newLatLngZoom(LOCATION_DEPAUW, 15);
                    map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
                    // Enabling MyLocation Layer of Google Map
                    map.setMyLocationEnabled(true);
                    map.getUiSettings().setZoomGesturesEnabled(true);
                    map.getUiSettings().setCompassEnabled(true);
                    map.getUiSettings().setRotateGesturesEnabled(true);
                    map.animateCamera(update);

                    /*
                     * for different colors:
                     * googlemap.addMarker(new MarkerOptions().position(new LatLng( 65.07213,-2.109375)).title("This is my title").snippet("and snippet").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ORANGE)));
                     * 
                     * .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ORANGE) is the key. change the HUE_ORANGE to anything that is available
                     * 
                     */

            } catch (Exception e) {
                Toast.makeText(getActivity(), e.toString(), Toast.LENGTH_LONG).show();
            }
 }

我已经正确导入了所有内容,并且地图在第一个视图上正确初始化。问题是当我导航到另一个片段然后回到这个地图片段时,我的应用程序崩溃了。我曾尝试使用eclipse模拟器,但无法正确配置它。有没有人知道在placeholderfragment中处理地图生命周期的方法?谢谢!

1 个答案:

答案 0 :(得分:1)

经过大量研究后,我发现有人设法妥善处理这个问题。

查看此github存储库:

https://gist.github.com/joshdholtz/4522551

MapView mapView;     GoogleMap地图;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.some_layout, container, false);

    // Gets the MapView from the XML layout and creates it
    mapView = (MapView) v.findViewById(R.id.mapview);
    mapView.onCreate(savedInstanceState);

    // Gets to GoogleMap from the MapView and does initialization stuff
    map = mapView.getMap();
    map.getUiSettings().setMyLocationButtonEnabled(false);
    map.setMyLocationEnabled(true);

    // Needs to call MapsInitializer before doing any CameraUpdateFactory calls
    try {
        MapsInitializer.initialize(this.getActivity());
    } catch (GooglePlayServicesNotAvailableException e) {
        e.printStackTrace();
    }

    // Updates the location and zoom of the MapView
    CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(new LatLng(43.1, -87.9), 10);
    map.animateCamera(cameraUpdate);

    return v;
}

@Override
public void onResume() {
    mapView.onResume();
    super.onResume();
}

@Override
public void onDestroy() {
    super.onDestroy();
    mapView.onDestroy();
}

@Override
public void onLowMemory() {
    super.onLowMemory();
    mapView.onLowMemory();
}

最初,我遇到了问题,因为我有多个片段可以切换,所以我在我这样做了:

public void onResume() {        

        try{    
            super.onResume();
            mapView.onResume();
            }catch(NullPointerException e){
                Log.d("onResume", "NullPointerException: " + e);
            }
    }

    @Override
    public void onDestroy() {
        try{
            super.onDestroy();
            mapView.onDestroy();
        }catch(NullPointerException e){
            Log.d("onDestroy", "NullPointerException: " + e);
        }
    }

    @Override
    public void onLowMemory() {
        try{
            super.onLowMemory();
            mapView.onLowMemory();
        }catch(NullPointerException e){
            Log.d("onLowMemory", "NullPointerException: " + e);
        }
    }