为什么我们的 dismiss 调用会导致 AndroidRuntimeException?

Why is our dismiss call causing an AndroidRuntimeException?

在我们的代码中,我们有这个简单的方法来关闭对话框,在我们的 DialogFragment:

实现中
public class OurDialogFragment extends OurBasicDialogFragment { // <= Line 15

    // ...

    protected OnClickListener _btnPositiveOnClickListenerWithClose = new OnClickListener() {
        @Override
        public void onClick(View v) {
            if (_btnPositiveListener != null) {
                _btnPositiveListener.onClick(OurDialogFragment.this, DialogInterface.BUTTON_POSITIVE);
            }

            dismissDialog(); // <= Line 30
        }
    };

    // ...

    private void dismissDialog() {
        if (OurDialogFragment.this.getDialog() != null) {
            SimpleAlertDialog.this.dismiss(); // <= line 58
        } else if (getOverrideDialog() != null) {
            getOverrideDialog().dismiss();
            setOverrideDialog(null);
        }
    }

    // ...
}

当我们对话框的 OK 按钮被按下时调用这个方法。但是,在指定的第 58 行,我们得到:

06-04 11:39:59.160  32694-32694/com.ourapp.app E/MainActivity﹕ UncaughtExceptionHandler
    android.util.AndroidRuntimeException: { what=3 when=-4s47ms } This message is already in use.
            at android.os.MessageQueue.enqueueMessage(MessageQueue.java:285)
            at android.os.Handler.enqueueMessage(Handler.java:618)
            at android.os.Handler.sendMessageAtTime(Handler.java:587)
            at android.os.Handler.sendMessageDelayed(Handler.java:558)
            at android.os.Handler.sendEmptyMessageDelayed(Handler.java:522)
            at android.os.Handler.sendEmptyMessage(Handler.java:507)
            at android.view.ViewRootImpl.die(ViewRootImpl.java:4079)
            at android.view.WindowManagerGlobal.removeViewLocked(WindowManagerGlobal.java:336)
            at android.view.WindowManagerGlobal.removeView(WindowManagerGlobal.java:286)
            at android.view.WindowManagerImpl.removeView(WindowManagerImpl.java:79)
            at android.app.Dialog.dismissDialog(Dialog.java:323)
            at android.app.Dialog.dismiss(Dialog.java:306)
            at android.app.DialogFragment.dismissInternal(DialogFragment.java:278)
            at android.app.DialogFragment.dismiss(DialogFragment.java:258)
            at com.ourapp.ui.OurDialogFragment.dismissDialog(OurDialogFragment.java:58)
            at com.ourapp.ui.OurDialogFragment.access[=11=]0(OurDialogFragment.java:15)
            at com.ourapp.ui.OurDialogFragment.onClick(OurDialogFragment.java:30)
            at android.view.View.performClick(View.java:4204)
            at android.view.View$PerformClick.run(View.java:17355)
            at android.os.Handler.handleCallback(Handler.java:725)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5041)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
            at dalvik.system.NativeStart.main(Native Method)

我们只编写(并且只能访问)此跟踪的 com.ourapp.ui.OurDialogFragment 部分中的代码。

既然我们不创建任何消息,更不用说尝试使用它们了,为什么会出现这个错误?

我们通过将以下内容添加到我们的消息处理程序 class 来解决这个问题,这并不明显,因为它没有在堆栈跟踪中被引用:

@Override
public void handleMessage(Message msg) {
    this.obtainMessage();

    // the rest of the method
}