我有一个导航抽屉,用supportmapfragment替换framelayout。我能够第一次加载片段,但再次按下该抽屉项会给出可怕的“重复二进制”错误。
我从Binary XML file line #9: Error inflating class fragment with Google Map Fragment读到错误的原因是,如果地图片段存在于xml中,则无法从导航器的片段中删除地图片段。他提出了一种程序化的方法,所以我尝试了这个:
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仍为空。这让我兴奋不已,我真的很感激能够持续访问导航器片段的正确解决方案,该片段不仅仅是地图,还有搜索栏和地图,就像我一样。
答案 0 :(得分:2)
出现此问题是因为地图已经退出并且您尝试使用相同的ID再次添加它。
因此,您需要删除onPause
中的地图框架,并且必须再次将其添加到onResume
中:
@Override
public void 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() {
super.onPause();
if (mMapFragment != null) {
FragmentTransaction fragmentTransaction = getChildFragmentManager().beginTransaction();
fragmentTransaction.remove(mMapFragment);
fragmentTransaction.commit();
}
}
将您的onStart
代码移至onActivityCreated
,如下所示:
mMapFragment = new SupportMapFragment() {
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
initMap();
}
};
public void initMap()
{
if (mMap== null) {
mMap= mMapFragment.getMap();
}
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));
}