android MapFragment不';替换时不显示
我在一个活动中有两个FrameLayout,我只是替换那些FrameLayout中的片段。 它适用于所有片段,但包含谷歌地图的MapFragment的片段除外。 当我第一次启动应用程序时,它工作了,我看到了地图(一切都工作了),但当我替换片段并将其替换回来时,我只得到一个空白,就像没有加载任何内容一样android MapFragment不';替换时不显示,android,google-maps,android-fragments,replace,mapfragment,Android,Google Maps,Android Fragments,Replace,Mapfragment,我在一个活动中有两个FrameLayout,我只是替换那些FrameLayout中的片段。 它适用于所有片段,但包含谷歌地图的MapFragment的片段除外。 当我第一次启动应用程序时,它工作了,我看到了地图(一切都工作了),但当我替换片段并将其替换回来时,我只得到一个空白,就像没有加载任何内容一样 public class MapFragment extends CustomFragment { private GoogleMap mMap; private Marker c
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正在工作,但是我在屏幕上没有看到任何东西(参见下面的屏幕截图)。
有人有主意吗?我真的不知道出了什么问题。我也有同样的问题。你找到原因了吗?谢谢。@Jiajun是的。不要破坏任何碎片,把它们藏起来。谢谢。它起作用了。