嗯,我在這裏有一個簡單的場景。Google Map Fragment + FragmentPagerAdapter拒絕協調
案例
1),將容納三個片段
@Override
public Fragment getItem(int position) {
Fragment fragment = null;
switch (position) {
case 0:
fragment=new LocationFragment();
break;
case 1:
fragment=new CongestFrag();
break;
case 2:
fragment=new SubcribedFrag();
break;
default:
break;
}
return fragment;
}
位置片段顧名思義甲FragmentStateAdapter,將容納谷歌地圖片段。造成這種情況的XML將是
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/off_white"
tools:context=".MainActivity" >
<TextView
android:id="@+id/fragment_title"
android:layout_width="@dimen/fragments_title_view_width"
android:layout_height="@dimen/fragments_title_view_height"
android:text="@string/location_fragment_title"
android:textSize="@dimen/fragments_title_text_size"
android:fontFamily="sans-serif-light"/>
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.MapFragment"/>
類似地,其它片段包含例如僅一個textviews。
問題
我們都知道,當我們着手或片段的列表中隱居了,viewpager niftly破壞並重新片段。這對我來說是很好的。我不希望使用setOffscreenPageLimit強制碎片留在內存中。
問題出在LocationFragment上。該應用程序以LocationFragment開始,它可以正確顯示地圖。這就是我如何進行再
LocationFragment - > CongestFrag - > SubcribedFrag
,一切工作正常。現在,當我倒過來。
SubscribedFrag-> CongestFrag。該計劃斷點OnCreateView爲LocationFragment擊中,它給該堆棧跟蹤
10-12 15:26:21.096: E/AndroidRuntime(17122): Caused by: java.lang.IllegalArgumentException: Binary XML file line #16: Duplicate id 0x7f060010, tag null, or parent id 0xffffffff with another fragment for com.google.android.gms.maps.MapFragment
這是打破了代碼(LocationFragment.java),當我來向LocationFragment從片列表
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
mFragmentView =inflater.inflate(R.layout.location_fragment, container, false);
//code breaks above
mMap.addMarker(new MarkerOptions().position(new LatLng(Globals.LocationFragment.DEFAULT_LAT, Globals.LocationFragment.DEFAULT_LONG)).title("Marker"));
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(Globals.LocationFragment.DEFAULT_LAT, Globals.LocationFragment.DEFAULT_LONG), 8.0f));
/*Just a container to keep the width and height . See usage*/
mSize = new Point();
initProperties();
initControls();
return mFragmentView;
}
底部
猜測 看來,該片段以前沒有被正確銷燬,並且android兩次膨脹相同的視圖。但是這並沒有道理。請幫助我。
謝謝
「我們都知道,當我們在片段列表中進行或撤退時,viewpager會立即銷燬並重新創建片段」 - 不,它不會。 'ViewPager'對碎片一無所知。 'FragmentPagerAdapter'和'FragmentStatePagerAdapter'知道片段,只有後者會根據頁面導航銷燬和重新創建這些片段。我不希望它只用三頁破壞任何東西。 – CommonsWare 2014-10-12 11:50:23
[重複ID,標記爲空或父ID與com.google.android.gms.maps.MapFragment的另一個片段](http://stackoverflow.com/questions/14083950/duplicate-id-tag-null -or-父-ID與 - 另一個片段換COM-谷歌-androi) – Simas 2014-10-12 12:04:12