[英]App crashes when opening Map Fragment twice
我正在設計具有向后兼容性的應用程序,因此我已經實現了支持和設計庫,以使用諸如導航抽屜之類的功能以及與新android版本相關的其他效果。
導航抽屜共管理5個片段。 這些片段之一包含一個映射:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="300dp"
class="com.google.android.gms.maps.MapFragment" />
-
public class LocationFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_location, container, false);
/*Try to obtain the map from the SupportMapFragment*/
mMap = ((MapFragment) getActivity().getFragmentManager().findFragmentById(R.id.map)).getMap();
這就是我在mainActivity上從導航抽屜管理片段的方式:
public class MainActivity extends AppCompatActivity implements DrawerLayout.DrawerListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
navView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override public boolean onNavigationItemSelected(MenuItem menuItem) {
menuItem.setChecked(true);
displayFragment(menuItem.getItemId());
drawerLayout.closeDrawers();
return true;
}
});
private void displayFragment(int itemId) {
Fragment fragment = null;
String title = getString(R.string.app_name);
switch (itemId) {
case R.id.navigation_item_1:
fragment = new StatusFragment();
title = getString(R.string.title_status);
break;
case R.id.navigation_item_2:
fragment = new LocationFragment();
title = getString(R.string.title_location);
break;
case R.id.navigation_item_3:
fragment = new SetupFragment();
title = getString(R.string.title_setup);
break;
case R.id.navigation_item_4:
fragment = new ProfileFragment();
title = getString(R.string.title_profile);
break;
case R.id.navigation_item_5:
fragment = new AboutFragment();
title = getString(R.string.title_about);
break;
default:
break;
}
if (fragment != null) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container_body, fragment);
fragmentTransaction.commit();
/*set the toolbar title*/
toolbar.setTitle(title);
}
}
問題是,如果我打開此地圖片段,則可以正常工作,但是如果我轉到另一個片段,然后又返回到地圖片段,則應用程序將崩潰,並在logcat中顯示以下內容:
android.view.InflateException: Binary XML file line #11: Error inflating class fragment
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
at com.masmr.detgprs.LocationFragment.onCreateView(LocationFragment.java:102)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1789)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:955)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1138)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:740)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1501)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:458)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5146)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:732)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalArgumentException: Binary XML file line #11: Duplicate id 0x7f0c0090, tag null, or parent id 0xffffffff with another fragment for com.google.android.gms.maps.MapFragment
at android.app.Activity.onCreateView(Activity.java:4791)
at
android.support.v7.app.AppCompatDelegateImplV11.callActivityOnCreateView(AppCompatDelegateImplV11.java:41)
at android.support.v7.app.AppCompatDelegateImplV7.onCreateView(AppCompatDelegateImplV7.java:826)
at android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:44)
at android.view.LayoutInflater$FactoryMerger.onCreateView(LayoutInflater.java:172)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:684)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
at com.masmr.detgprs.LocationFragment.onCreateView(LocationFragment.java:102)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1789)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:955)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1138)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:740)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1501)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:458)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5146)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:732)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)
at dalvik.system.NativeStart.main(Native Method)
102行引用地圖片段的onCreateView()的第一行,在該行中我將map_layout膨脹。
只需在Fragment
實現
@Override
public void onDestroyView ()
{
try{
MapFragment fragment = ((MapFragment) getFragmentManager().findFragmentById(R.id.map));
FragmentTransaction ft = getActivity().getFragmentManager().beginTransaction();
ft.remove(fragment);
ft.commit();
}catch(Exception e){
}
super.onDestroyView();
}
您可以這樣做:
fragment_map.xml :
<com.google.android.gms.maps.MapView
android:id="@+id/mapView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
MyMapFragment.java :
private GoogleMap mMap;
private MapView mapView;
在Fragment的 onCreaeView()上:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
initializaMap(rootView, savedInstanceState);
}
private void initializaMap(View rootView, Bundle savedInstanceState){
MapsInitializer.initialize(getActivity());
switch (GooglePlayServicesUtil.isGooglePlayServicesAvailable(getActivity())) {
case ConnectionResult.SUCCESS:
mapView = (MapView) rootView.findViewById(R.id.mapView);
mapView.onCreate(savedInstanceState);
if (mapView != null) {
mMap = mapView.getMap();
mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
UiSettings mUiSettings = mMap.getUiSettings();
mMap.setMyLocationEnabled(true);
mMap.animateCamera(CameraUpdateFactory.zoomTo(7.0f));
mUiSettings.setCompassEnabled(true);
mUiSettings.setMyLocationButtonEnabled(false);
}
break;
case ConnectionResult.SERVICE_MISSING:
break;
case ConnectionResult.SERVICE_VERSION_UPDATE_REQUIRED:
break;
default:
}
}
在onResume()上 :
@Override
public void onResume() {
super.onResume();
if(mapView!=null){
mapView.onResume();
}
}
在onDestroy()上 :
@Override
public void onDestroy() {
super.onDestroy();
if(mapView!=null){
mapView.onDestroy();
}
}
在onLowMemory()上 :
@Override
public void onLowMemory() {
super.onLowMemory();
if(mapView!=null){
mapView.onLowMemory();
}
}
完成
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.