2014-08-28 96 views
1

我有一個導航抽屜,用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仍然爲空。這是讓我瘋狂的,我真的很感謝適當的解決方案,不斷地訪問一個不僅僅是地圖,而是像我一樣的搜索欄和地圖的導航器片段。

+0

嘗試在'onPause()'方法中刪除你的Google地圖,並在'onResume()'方法中再次添加。 – Piyush 2014-08-30 04:50:15

+0

使用[MapView](https://developer.android.com/reference/com/google/android/gms/maps/MapView.html)而不是'MapFragment'。 – kId 2014-08-30 06:58:12

回答

2

此問題的原因是地圖已經存在,並嘗試用相同的ID重新添加它.. 所以,你需要刪除地圖framegnt在並且必須在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)); 
    } 
+0

從我+1 .. !!! – Piyush 2014-08-30 04:56:39

+0

謝謝,我添加了所有這些(用當前的mMap替換mGoogleMap),onStart中的mMap仍然爲空,導致空指針異常。我已在OP中發佈了修訂的完整代碼。謝謝 – 2014-08-30 06:40:46

+0

havnt你重寫'onActivityCreated' ???刪除這行'mMapFragment = SupportMapFragment.newInstance();'... @HukeLau_DABA – 2014-08-30 06:47:04