我有一個導航抽屜,用supportmapfragment替換framelayout。我能夠第一次加載片段,但再次按下該抽屜項目會給出可怕的「重複二進制」錯誤。谷歌地圖碎片在另一個片段
我從Binary XML file line #9: Error inflating class fragment with Google Map Fragment中讀到這個錯誤是因爲如果映射片段存在於xml中,那麼映射片段不能從navdrawer的片段中移除。他建議的方案做法,所以我嘗試這樣做:
NavDrawerFirstFragment:
@Override
public void onCreate(Bundle savedInstanceState) {
Log.d("DEBUG", "In NavDrawerFirstFragment onCreate()");
super.onCreate(savedInstanceState);
}
public NavDrawerFirstFragment() {
// fragment needs a no arg ctor
}
public static NavDrawerFirstFragment newInstance(Location currentLoc) {
NavDrawerFirstFragment firstFragment = new NavDrawerFirstFragment();
// how to set arguments for this fragment when this method is static
Bundle args = new Bundle();
args.putParcelable("current_loc", currentLoc);
firstFragment.setArguments(args);
return firstFragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.d("DEBUG", "NavDrawerFirstFragment onCreateView()");
super.onCreateView(inflater, container, savedInstanceState);
View view = inflater.inflate(R.layout.fragment_nav_drawer_first, container, false);
if (mMap == null) {
mMapFragment = new SupportMapFragment();
FragmentTransaction ft = getChildFragmentManager().beginTransaction();
ft.replace(R.id.mapContainer, mMapFragment);
mMap = mMapFragment.getMap();
}
/*if(mMap == null) {
mMapFragment = (SupportMapFragment)getFragmentManager().findFragmentById(R.id.map);
}*/
return view;
}
@Override
public void onStart() {
Log.d("DEBUG", "NavDrawerFirstFragment onStart()");
super.onStart();
mMap.getUiSettings().setRotateGesturesEnabled(false);
// check for google play services here
MarkerOptions mo = new MarkerOptions();
Location initialLoc = getArguments().getParcelable("current_loc");
LatLng latlong = new LatLng(initialLoc.getLatitude(), initialLoc.getLongitude());
Marker startingMarker = mMap.addMarker(mo.position(latlong).position(latlong).icon(
BitmapDescriptorFactory.defaultMarker(
BitmapDescriptorFactory.HUE_GREEN)));
startingMarker.setDraggable(true);
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latlong, 12));
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
Log.d("DEBUG", "NavDrawerFirstFragment onActivityCreated");
super.onActivityCreated(savedInstanceState);
if (mMap == null) {
mMap = mMapFragment.getMap();
}
}
@Override
public void onDetach() {
Log.d("DEBUG", "NavDrawerFirstFragment onDetach()");
super.onDetach();
}
@Override
public void onResume() {
Log.d("DEBUG", "NavDrawerFirstFragment onResume()");
super.onResume();
FragmentTransaction fragmentTransaction = getChildFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.mapContainer, mMapFragment);
fragmentTransaction.commit();
if (mMapFragment != null)
mMap = mMapFragment.getMap();
}
@Override
public void onPause() {
Log.d("DEBUG", "NavDrawerFirstFragment onPause()");
super.onPause();
if (mMapFragment != null) {
FragmentTransaction fragmentTransaction = getChildFragmentManager().beginTransaction();
fragmentTransaction.remove(mMapFragment);
fragmentTransaction.commit();
}
}
fragment_nav_drawer_first.xml:
<?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="wrap_content"
android:orientation="vertical">
<LinearLayout
android:id="@+id/navBar"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/et_location"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="@string/hnt_et_location"
android:layout_weight="1"
android:inputType="text" />
<Button
android:id="@+id/btn_find"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/str_btn_find" />
</LinearLayout>
<FrameLayout
android:id="@+id/mapContainer"
android:layout_width="fill_parent"
android:layout_height="wrap_content"></FrameLayout>
</LinearLayout>
這其實是在另一個SO後提出的解決方案:Error opening SupportMapFragment for second time
當我調試它時,我注意到,無論我在進入onStart()之前等了多久,我的GoogleMap mMap仍然爲空。這是讓我瘋狂的,我真的很感謝適當的解決方案,不斷地訪問一個不僅僅是地圖,而是像我一樣的搜索欄和地圖的導航器片段。
嘗試在'onPause()'方法中刪除你的Google地圖,並在'onResume()'方法中再次添加。 – Piyush 2014-08-30 04:50:15
使用[MapView](https://developer.android.com/reference/com/google/android/gms/maps/MapView.html)而不是'MapFragment'。 – kId 2014-08-30 06:58:12