我在自己的Fragment中有SupportMapFragment。当我第二次显示这个片段时,它会崩溃。
起初我尝试在XML中添加SupportMapFragment,但我遇到了与其描述的here相同的问题。
所以我尝试将其添加到programmaticaly中:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.map_fragment, container, false);
isGooglePlay=isGooglePlay();
if(isGooglePlay){
mMapFragment = SupportMapFragment.newInstance();
FragmentTransaction fragmentTransaction =
getChildFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.map_fragment_mapRoot, mMapFragment);
fragmentTransaction.commit();
map = mMapFragment.getMap();
if (map != null){
map.animateCamera(CameraUpdateFactory.zoomTo(100F));
}
}
return view;
}
它在提交事务的行崩溃。
FATAL EXCEPTION: main
Process: com.sabatsoft.sgs14remote, PID: 23914
java.lang.IllegalStateException: Activity has been destroyed
at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1365)
at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:595)
at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:574)
at com.sabatsoft.sgs14remote.fragment.FlightMapFragment.onCreateView(FlightMapFragment.java:45)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:440)
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:5001)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
答案 0 :(得分:1)
有一个类似的已知问题,请看here
这是嵌套片段中的错误。基本上,子FragmentManager在与活动分离时最终会出现内部状态损坏。
短期解决方法是在片段中添加以下代码。
@Override
public void onDetach() {
super.onDetach();
try {
Field childFragmentManager = Fragment.class.getDeclaredField("mChildFragmentManager");
childFragmentManager.setAccessible(true);
childFragmentManager.set(this, null);
} catch (NoSuchFieldException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}