Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DialogFragment.dismiss crashing with NullPointerException

Tags:

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

specifically at the line: FragmentTransaction ft = getFragmentManager().beginTransaction();

like image 839
LuxuryMode Avatar asked May 10 '12 02:05

LuxuryMode


People also ask

Is DialogFragment deprecated?

This class was deprecated in API level 28. Use the Support Library DialogFragment for consistent behavior across all devices and access to Lifecycle.

How do you dismiss a DialogFragment?

tl;dr: The correct way to close a DialogFragment is to use dismiss() directly on the DialogFragment. Control of the dialog (deciding when to show, hide, dismiss it) should be done through the API here, not with direct calls on the dialog. Dismiss the fragment and its dialog.

What is the difference between dialog & DialogFragment?

Show activity on this post. Dialog: A dialog is a small window that prompts the user to make a decision or enter additional information. DialogFragment: A DialogFragment is a special fragment subclass that is designed for creating and hosting dialogs.

How do you pass arguments to DialogFragment?

you can set your args. class IntervModifFragment : DialogFragment(), ModContract. View { companion object { fun newInstance( plom:String,type:String,position: Int):IntervModifFragment { val fragment =IntervModifFragment() val args = Bundle() args. putString( "1",plom) args.


2 Answers

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...

like image 121
pretty angela Avatar answered Oct 09 '22 02:10

pretty angela


Simplest solution is to check "getFragmentManager()" for "null" before calling "dismiss()" method. Also you can extend "DialogFragment" class and override method "dismiss()" to check it there:

@Override public void dismiss() {     if (getFragmentManager() != null) super.dismiss(); } 
like image 43
Ruslan Yanchyshyn Avatar answered Oct 09 '22 03:10

Ruslan Yanchyshyn