BadTokenException甚至在引用Activity而不是应用程序上下文之后

时间:2013-10-08 06:18:50

标签: java android android-dialog

android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@406a6678 is not valid; is your activity running?
at android.view.ViewRoot.setView(ViewRoot.java:528)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
at android.view.Window$LocalWindowManager.addView(Window.java:424)
at android.app.Dialog.show(Dialog.java:241)
at android.app.Activity.showDialog(Activity.java:2569)
at android.app.Activity.showDialog(Activity.java:2527)
at MyCode$8$4.run(MyCode.java:557)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3683)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:878)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:636)
at dalvik.system.NativeStart.main(Native Method)

执行以下代码时,我遇到异常。处理完成后,将显示此文件对话框,并且进度条达到100%。 FileSaveDialog扩展了Dialog并实现了OnCompletionListener

runOnUiThread(new Runnable() {
    @Override
    public void run() {
        showDialog(error.Code());//Line 557
    }
});

    @Override
protected Dialog onCreateDialog(int id) {
    Dialog dialog;
    AlertDialog.Builder builder;
    final ScrollView scrollView = new ScrollView(this);
    final TextView textView = new TextView(this);

    switch (id) {
    // Other cases are here
    case 4:
        File playFile = new File(mediaPath, TEMP_WAV_FILE_NAME);

        dialog = new FileSaveDialog(this, getResources(),
                playFile.getAbsolutePath(), saveDiscardHandler);
        dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
            @Override
            public void onCancel(DialogInterface dialog) {
                // do whatever you want the back key to do
                cleanUp();
            }
        });

        break;
    // Other cases are here
    default:
        dialog = null;
    }
    return dialog;
}

1 个答案:

答案 0 :(得分:1)

您必须检查活动isFinishing()如果活动结束,则返回true; else返回false。