如何解决:android.view.WindowManager $ BadTokenException - 你的活动是在运行吗?

时间:2015-07-16 14:46:11

标签: android webview dialog alertdialog

我有一个活动,包括很多Dialogs,对话框为Dialog_1,Dialog_2,Dialog_3,我在其中一个Dialogs中实现了一个WebView,让我们说Dialog_2。在对话框中导航时,第一次打开带有WebView的Dialog_2没有问题,但第二次导航到Dialog_3并返回Dialog_2或Dialog_1到Dialog_2并触摸WebView时,我收到如下所述的错误。

我查看了Bad token exception,但仍无法使用

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

致命的例外:主要     android.view.WindowManager $ BadTokenException:无法添加窗口 - 令牌android.view.ViewRootImpl$W@41ec0fc8无效;你的活动在运行吗?

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)

对话框代码是

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();
                }

如何解决此错误。非常感谢您的帮助和建议。

1 个答案:

答案 0 :(得分:2)

如果您的活动由于某种原因而被杀死,并且您的活动的窗口令牌已过期,但您仍尝试访问它,则会发生这种情况。所以要解决这个问题,请将其放在onPause

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

     if ((progress != null) && progress.isShowing()) {
        progress.dismiss();
     }
     progress = null;
 }