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

时间:2015-06-04 16:25:36

标签: android runtimeexception

在我们的代码中,我们使用这个简单的方法来解除对话框,在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$000(OurDialogFragment.java:15)
            at com.ourapp.ui.OurDialogFragment$1.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部分中的代码。

由于我们不会创建任何消息,更不用说尝试使用它们了,为什么会出现此错误?

1 个答案:

答案 0 :(得分:0)

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

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

    // the rest of the method
}