0

When I try to dissmiss a FragmentDialog, my app crashes sometimes.
Here's the log:

Process: com.xxx, PID: 9981
java.lang.RuntimeException: Error receiving broadcast Intent { act=xxxx flg=0x10 (has extras) } in xxxxxx
    at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:893)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:148)
    at android.app.ActivityThread.main(ActivityThread.java:5438)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:629)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.app.FragmentTransaction android.app.FragmentManager.beginTransaction()' on a null object reference
    at android.app.DialogFragment.dismissInternal(DialogFragment.java:296)
    at android.app.DialogFragment.dismissAllowingStateLoss(DialogFragment.java:277)
    at xxxxx.updateStatus(BaseAppCompatActivity.java:96)
    at xxxxx.access$000(BaseAppCompatActivity.java:43)
    at xxxxx$1.onReceive(BaseAppCompatActivity.java:79)
    at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:883)

xxxxx.updateStatus(BaseAppCompatActivity.java:96)
this line is try to dismiss a DialogFragment:

mDialogFragment.dismissAllowingStateLoss();


A DialogFragment is in my Activity.

private SimpleBlockedDialogFragment mDialogFragment = SimpleBlockedDialogFragment.newInstance();

So mDialogFragment is not null.

I show dialog like this:

FragmentTransaction ft = getFragmentManager().beginTransaction();
mDialogFragment.updateMessage("xxx");
mDialogFragment.show(ft, "block_dialog");

And dismiss dialog like this:

mDialogFragment.dismissAllowingStateLoss();

Sometimes, I show dialog only once, but dismiss more than once. But I don't think that cause a crash.

Here's the dismissInternal function in the DialogFragment:

void dismissInternal(boolean allowStateLoss) {
    if (mDismissed) {
        return;
    }
    mDismissed = true;
    mShownByMe = false;
    if (mDialog != null) {
        mDialog.dismiss();
        mDialog = null;
    }
    mViewDestroyed = 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();
        }
    }
}

log tells me that getFragmentManager() return null. I don't know why this happened.
How to dismiss a FragmentDialog correctly?

update:
I try android.support.v4.app.FragmentTransaction, and still get an exception.

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.support.v4.app.FragmentTransaction android.support.v4.app.FragmentManager.beginTransaction()' on a null object reference
    at android.support.v4.app.DialogFragment.dismissInternal(DialogFragment.java:196)
    at android.support.v4.app.DialogFragment.dismissAllowingStateLoss(DialogFragment.java:177)

Help~

android_su
  • 1,637
  • 4
  • 21
  • 30

1 Answers1

0

Try getSupportFragmentManager().beginTransaction(); this may help.

UserSharma
  • 458
  • 6
  • 20
  • thanks, I will try. But why do I use android.support.v4.app.FragmentTransaction instead of android.app.FragmentTransaction? – android_su May 21 '16 at 10:31