DialogFragment.dismiss crashing with NullPointerException

后端 未结 6 2006
慢半拍i
慢半拍i 2021-02-18 13:08

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

6条回答
  •  执笔经年
    2021-02-18 13:27

    This may also occur when you call dismiss() before you have called show() like Sogger said.

    After Dialog object is constructed but before dialog is not showed, if (mDialog != null) can be passed and NullPointerException will occur.

    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;
    }
    

    I think that mDialog.isAdded() condition might be enough...

提交回复
热议问题