我正在使用framelayout显示片段
<FrameLayout
android:id="@+id/fragment_content"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="@+id/bottom_layout"
android:background="@android:color/white" >
</FrameLayout>
为了打开碎片我用这个
ft.add(R.id.fragment_content, frag);
ft.commit();
fm.beginTransaction();
一个片段包含map,所以我在布局文件中使用这些行
<fragment
android:id="@+id/trafic_Alarm_main_mapView"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.Utilities.MySupportMapFragment"
android:layout_alignParentTop="true" />
这是我的Java代码
try{
mMap=(MySupportMapFragment)TabsActivity.fm.findFragmentById(R.id.trafic_Alarm_main_mapView)).getMap();
}catch (Exception e){
mMap=((MySupportMapFragment)getChildFragmentManager().findFragmentById(R.id.trafic_Alarm_main_mapView)).getMap();
}
现在,如果getChildFragmentManager()被调用,并且我切换到另一个使用getSupportFragmentManager开始事务的片段,则会导致崩溃。验证日志
01-16 18:36:15.690 7294-7294/com.TrafikAlarm E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.TrafikAlarm, PID: 7294
java.lang.IllegalStateException: Recursive entry to executePendingTransactions
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1461)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:454)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
我正在使用android studio编译这些库,
compile 'com.google.android.gms:play-services:6.5.87'
compile 'com.android.support:appcompat-v7:21.0.3'
当我使用eclipse时,我不需要使用getchildfragmentmanager,一切正常。
提前谢谢。
最佳答案
You cannot inflate a layout into a fragment when that layout includes a fragment. Nested fragments are only supported when added to a fragment dynamically.
FILL_PARENT (renamed MATCH_PARENT in API Level 8 and higher).