0

I have an activity with lot of Dialogs for ex Dialog_1, Dialog_2, Dialog_3, I have implemented a WebView in one of the Dialogs lets say Dialog_2. When navigate through the Dialog's, first time the Dialog_2 with the WebView opens with no problem, but second time when I navigate to Dialog_3 and come back to Dialog_2 OR Dialog_1 to Dialog_2 and touch the WebView I get an error as mentioned bellow.

I have looked at Bad token exception but it does not work still using

     if (! this.isFinishing()) {
        showDialog(DIALOG_ADD_A_PERSON_BODY_MAP)
    }

FATAL EXCEPTION: main android.view.WindowManager$BadTokenException: Unable to add window -- token android.view.ViewRootImpl$W@41ec0fc8 is not valid; is your activity running?

FATAL EXCEPTION: main
android.view.WindowManager$BadTokenException: Unable to add window -- token android.view.ViewRootImpl$W@41ec0fc8 is not valid; is your activity running?
        at android.view.ViewRootImpl.setView(ViewRootImpl.java:806)
        at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:265)
        at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:73)
        at android.widget.ZoomButtonsController.setVisible(ZoomButtonsController.java:373)
        at android.webkit.ZoomControlEmbedded.show(ZoomControlEmbedded.java:41)
        at android.webkit.ZoomManager.invokeZoomPicker(ZoomManager.java:1728)
        at android.webkit.WebViewClassic.startDrag(WebViewClassic.java:11645)
        at android.webkit.WebViewClassic.handleTouchEventCommon(WebViewClassic.java:11170)
        at android.webkit.WebViewClassic.onHandleUiTouchEvent(WebViewClassic.java:3225)
        at android.webkit.WebViewClassic.onHandleUiEvent(WebViewClassic.java:3135)
        at android.webkit.WebViewClassic.access$12000(WebViewClassic.java:278)
        at android.webkit.WebViewClassic$PrivateHandler.dispatchUiEvent(WebViewClassic.java:13405)
        at android.webkit.WebViewInputDispatcher.dispatchUiEvent(WebViewInputDispatcher.java:1078)
        at android.webkit.WebViewInputDispatcher.dispatchUiEvents(WebViewInputDispatcher.java:1066)
        at android.webkit.WebViewInputDispatcher.access$300(WebViewInputDispatcher.java:78)
        at android.webkit.WebViewInputDispatcher$UiHandler.handleMessage(WebViewInputDispatcher.java:1392)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:176)
        at android.app.ActivityThread.main(ActivityThread.java:5365)
        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:1102)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
        at dalvik.system.NativeStart.main(Native Method)

The Dialog code is

case DIALOG_ADD_A_PERSON_BODY_MAP:
                try {
                    LayoutInflater inflaterBodyMap = this
                            .getLayoutInflater();
                    final View inflatorBodyMap = inflaterBodyMap
                            .inflate(
                                    R.layout.dialog_incident_window_body_map,
                                    null);
                    bodyMapWebView  = (WebView) inflatorBodyMap.findViewById(R.id.webView);
                    bodyMapWebView.getSettings().setJavaScriptEnabled(true);
                    //bodyMapWebView.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
                    //bodyMapWebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
                    bodyMapWebView.getSettings().setBuiltInZoomControls(true);
                    bodyMapWebView.getSettings().setSupportZoom(true);

                    bodyMapWebView.loadUrl("https://www.google.co.uk");

                    bodyMapWebView.addJavascriptInterface(new WebAppInterface(this), "Android");                    
                    builder.setTitle("Injured person");
                    builder.setView(inflatorBodyMap);

                    builder.setPositiveButton("Next",
                            new DialogInterface.OnClickListener() {

                                @Override
                                public void onClick(DialogInterface dialog,
                                                    int which) {
                                    showDialog(DIALOG_ADD_A_PERSON_WAS_THE_PERSON_INJURED_ET);
                                    wasThePersonInjured.setText(map_incident_addAPerson.get("InjuredNotes"));


                                }
                            });
                    builder.setNegativeButton("Back",
                            new DialogInterface.OnClickListener() {

                                @Override
                                public void onClick(DialogInterface dialog,
                                                    int which) {


                                    showDialog(DIALOG_ADD_A_PERSON_WAS_THE_PERSON_INJURED);

                                }
                            });
                    return builder.create();
                } catch (Exception e) {
                    e.printStackTrace();
                }

How do I resolve this error. Your help and suggestions much appreciated.

Community
  • 1
  • 1
BRDroid
  • 3,920
  • 8
  • 65
  • 143

1 Answers1

2

This happens if your Activity has been killed due to some reason and window token of your activity has expired but you try and access it anyways. So to solve this put this in onPause:

 @Override
 protected void onPause() {
     super.onPause();

     if ((progress != null) && progress.isShowing()) {
        progress.dismiss();
     }
     progress = null;
 }
Scott Marchant
  • 3,447
  • 2
  • 22
  • 29
user1530779
  • 409
  • 5
  • 8
  • Hi thank you for your response, what are you referring to as 'progress'? – BRDroid Jul 16 '15 at 14:58
  • progress is your dialog instance can be a ProgressDialog or just Dialog – user1530779 Jul 16 '15 at 16:01
  • when I did that, now i do not get an error, but what happens is when i am in that dialog and i lock the screen, it dismisses the dialog. I don't want to dismiss the dialog when the user locks the screen. – BRDroid Jul 17 '15 at 10:50
  • Added this bit of code and it works, is this the right way of doing it. if ((alertBodyMap == null) && !alertBodyMap.isShowing()){ alertBodyMap.dismiss(); alertBodyMap = null; } – BRDroid Jul 17 '15 at 10:59