[英]IllegalStateException: Fragment already added
I got this error: 我收到此错误:
java.lang.IllegalStateException: Fragment already added: MenuBottomSheetFragment{d476429 #0}
at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1891)
at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:760)
My code when open clicked MenuBottomSheetFragment
: 我的代码在打开时单击了
MenuBottomSheetFragment
:
private void openBottomSheet() {
if (mMenuBottomSheetFragment == null)
mMenuBottomSheetFragment = new MenuBottomSheetFragment();
if (!mMenuBottomSheetFragment.isShowing())
mMenuBottomSheetFragment.show(getSupportFragmentManager(), mMenuBottomSheetFragment.getTag());
}
And my MenuBottomSheetFragment
have nothing special. 而且我的
MenuBottomSheetFragment
没有什么特别的。
I really have no idea why. 我真的不知道为什么。 Most of the time,
MenuBottomSheetFragment
works fine. 大多数情况下,
MenuBottomSheetFragment
可以正常工作。 But some time, it throws this Exception then I do nothing. 但是有一段时间,它抛出此异常,然后我什么也不做。
So what is the problem in my case? 那么我的问题是什么呢? And how can I fix it?
我该如何解决?
Replace the isShowing with this: 将isShowing替换为:
if(!mMenuBottomSheetFragment.isAdded()) {
mMenuBottomSheetFragment.show(getSupportFragmentManager(), mMenuBottomSheetFragment.getTag());
}
The fragment has already been added. 该片段已被添加。 There is no need to show it again.
无需再次显示。 Just check it after the
null
-check and return if it's added: 只需在
null
-check之后检查它,然后返回是否添加了它:
if(mMenuBottomSheetFragment.isAdded()) {
return;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.