我一直在尝试android.support.v4.widget.DrawerLayout
并从抽屉中选择 4 个碎片。地图最初加载没有问题,但是当我打开抽屉并更改片段时,我无法返回地图。我只是得到一个黑屏。Logcat 显示片段已重新创建,但我什么也没得到。只是一个空白的黑屏。我可以毫无问题地在其他片段之间切换。我究竟做错了什么?我的项目的最小 API 为 14。
我从这里加载ExploreMap
(一个片段)MainActivity.java
:
if (position == 0){
ExploreMap exMap = new ExploreMap();
exMap.setRetainInstance(true);
getFragmentManager().beginTransaction().replace(R.id.content_frame, exMap).commit();
}
在ExploreMap.java
我做以下
public class ExploreMap extends Fragment implements OnInfoWindowClickListener, android.location.LocationListener, OnMapLongClickListener{
private LocationManager mLocManager;
private GoogleMap mMap;
private MapFragment mMapFragment;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
FragmentManager fm = getActivity().getFragmentManager();
mMapFragment = (MapFragment) fm.findFragmentById(R.id.map);
if (mMapFragment == null) {
mMapFragment = MapFragment.newInstance();
fm.beginTransaction().replace(R.id.map, mMapFragment).commit();
}
if (savedInstanceState == null) {
// First incarnation of this activity.
mMapFragment.setRetainInstance(true);
}else {
// Reincarnated activity. The obtained map is the same map instance in the previous
// activity life cycle. There is no need to reinitialize it.
mMap = mMapFragment.getMap();
}
createMapIfNeeded();
return inflater.inflate(R.layout.explore_map_layout, container, false);
}
@Override
public void onResume() {
super.onResume();
//create the map
createMapIfNeeded();
}
private void createMapIfNeeded(){
if(mLocManager == null){
mLocManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
mLocManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES,
this);
}
//locmanager can return null if no last known locaiton is available.
location = mLocManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
//if a map has not already been instantiated it'll return null
if(mMap == null){
//instantiate map
mMap = mMapFragment.getMap();
//check it has been instantiated
if(mMap != null){
mMap.setOnMapLongClickListener(this);
mMap.setOnInfoWindowClickListener(this);
mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
mMap.setMyLocationEnabled(true);
//Manipulate map here (add coordinates/polylines from trip etc etc.)
UiSettings setting = mMap.getUiSettings();
setting.setTiltGesturesEnabled(true);
setting.setRotateGesturesEnabled(true);
setting.setZoomControlsEnabled(true);
setting.setMyLocationButtonEnabled(true);
if(location != null){
CameraUpdate cu = CameraUpdateFactory.newLatLngZoom(new LatLng(location.getLatitude(), location.getLongitude()), 15);
mMap.animateCamera(cu);
}
}
}
}
XML如下
mainactivity_layout.xml
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- As the main content view, the view below consumes the entire
space available using match_parent in both dimensions. -->
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- android:layout_gravity="start" tells DrawerLayout to treat
this as a sliding drawer on the left side for left-to-right
languages and on the right side for right-to-left languages.
The drawer is given a fixed width in dp and extends the full height of
the container. A solid background is used for contrast
with the content view. -->
<ListView
android:id="@+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp"
android:background="#111"/>
</android.support.v4.widget.DrawerLayout>
和exploremap_layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<FrameLayout
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>