DialogFragment.dismiss与NullPointerException崩溃 [英] DialogFragment.dismiss crashing with NullPointerException
问题描述
我正在做一些背景工作,并在执行此操作时显示DialogFragment.一旦完成工作并调用了相关的回调,我便关闭了该对话框.当我这样做时,我在android源代码中遇到了由NPE引起的崩溃:
I'm doing some background work and showing a DialogFragment while I do that. Once my work is done and the relevant callback is invoked, I dismiss the dialog. When I do, I get a crash caused by a NPE in the android source, here:
void dismissInternal(boolean allowStateLoss) {
if (mDialog != null) {
mDialog.dismiss();
mDialog = null;
}
mRemoved = true;
if (mBackStackId >= 0) {
getFragmentManager().popBackStack(mBackStackId,
FragmentManager.POP_BACK_STACK_INCLUSIVE);
mBackStackId = -1;
} else {
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.remove(this);
if (allowStateLoss) {
ft.commitAllowingStateLoss();
} else {
ft.commit();
}
}
}
具体在以下行: FragmentTransaction ft = getFragmentManager().beginTransaction();
推荐答案
当您像Sogger所说的那样调用show()之前调用dismiss()时,也会发生这种情况.
This may also occur when you call dismiss() before you have called show() like Sogger said.
在构造Dialog对象之后但不显示对话框之前,如果可以传递(mDialog!= null)并发生NullPointerException.
After Dialog object is constructed but before dialog is not showed, if (mDialog != null) can be passed and NullPointerException will occur.
当您检查mDialog是否为空时,
When you check if mDialog is null or not,
if (mDialog != null) {
mDialog.dismiss();
mDialog = null;
}
添加更多类似如下的条件,
Add more conditions like below,
if ((mDialog != null) && mDialog.isAdded() && mDialog.isResumed()) {
mDialog.dismiss();
mDialog = null;
}
我认为mDialog.iwego()条件可能就足够了...
I think that mDialog.isAdded() condition might be enough...
这篇关于DialogFragment.dismiss与NullPointerException崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!