android MapFragment在替换时不显示

时间:2014-06-01 21:57:15

标签: android google-maps android-fragments replace mapfragment

我在一个活动中有两个FrameLayout,我只是替换那些FrameLayout中的碎片。 它适用于每个片段,但包含MapMap的MapFragment。 当我第一次启动应用程序时,它工作,我看到地图(一切正常),但当我更换片段并将其替换回来时,我只是得到一个空白空间,就像什么都没有加载。

public class MapFragment extends CustomFragment {
    private GoogleMap mMap;
    private Marker currentMarker;
    private OnFragmentInteractionListener mListener;
    public static final String ID = "MAP_FRAGMENT";
    private static View view;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = null;
        try {
            view = inflater.inflate(R.layout.fragment_map, container, false);
        } catch (InflateException e) {
            Log.d("","");
        }finally{
            setUpMapIfNeeded();
            if(view!=null)this.view=view;
            return this.view;
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        Fragment f = getFragmentManager().findFragmentById(R.id.map);
        if (f != null)getActivity().getFragmentManager().beginTransaction().remove(f).commit();
        mMap=null;
        currentMarker=null;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            mListener = (OnFragmentInteractionListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    private void setUpMapIfNeeded() {
        // Do a null check to confirm that we have not already instantiated the map.
        if (mMap == null) {
            // Try to obtain the map from the SupportMapFragment.
            mMap = ((com.google.android.gms.maps.MapFragment)getFragmentManager().findFragmentById(R.id.map)).getMap();
            // Check if we were successful in obtaining the map.
            if (mMap != null) {
                setUpMap();
            }
        }
    }

    private void setUpMap() {
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public void update(FragmentMessage message) {
        if(mMap!=null) {
            Location loc = (Location) message.getMessages()[0];
            if (currentMarker != null)
                currentMarker.remove();
            currentMarker = mMap.addMarker(new MarkerOptions().position(new LatLng(loc.getLatitude(), loc.getLongitude())).title("Marker"));
            CameraPosition cameraPosition = new CameraPosition.Builder()
                    .target(new LatLng(loc.getLatitude(), loc.getLongitude()))      // Sets the center of the map to Mountain View
                    .zoom(15)                   // Sets the zoom
                    .build();                   // Creates a CameraPosition from the builder
            mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
        }
    }
}

mMap永远不会为空,所以MapFragment正在工作,我想但我在屏幕上看不到任何内容(见下面的截图)。 At app launch After switching from another fragment

有没有人有想法?我真的不知道出了什么问题。

0 个答案:

没有答案