0
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)

I am getting above exception when following code is executed. This file dialog will shown once the processing is done and progressbar reaches 100%. FileSaveDialog extends Dialog and implements 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;
}
Anonymous Me
  • 938
  • 1
  • 8
  • 26
  • Follow the [link](http://stackoverflow.com/questions/10429213/windowmanagerbadtokenexception-in-android/10429486#10429486) it may help you.. – vishesh chandra Oct 08 '13 at 06:23
  • You can not show the dialog in thread. Please remove the handler code and just simple show the dialog. The dialog runs in UI so do not try to show it in separate thread. – GrIsHu Oct 08 '13 at 06:28
  • Try [this](http://stackoverflow.com/a/18665887/1554935). It will help you for sure. – Ritesh Gune Oct 08 '13 at 06:29

1 Answers1

1

You must check activity isFinishing() If the activity is finishing, returns true; else returns false.

Paresh Mayani
  • 122,920
  • 69
  • 234
  • 290
nurisezgin
  • 1,310
  • 10
  • 15