1

我试图让我的 webview 显示一个 javascript 'confirm' 框,并且我得到以下异常(来自 logcat):

E/AndroidRuntime(  387): android.view.WindowManager$BadTokenException: Unable to add         window -- token android.os.BinderProxy@40
5113d8 is not valid; is your activity running?
E/AndroidRuntime(  387):        at android.view.ViewRoot.setView(ViewRoot.java:527)
E/AndroidRuntime(  387):        at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
E/AndroidRuntime(  387):        at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
E/AndroidRuntime(  387):        at android.view.Window$LocalWindowManager.addView(Window.java:424)
E/AndroidRuntime(  387):        at android.app.Dialog.show(Dialog.java:241)
E/AndroidRuntime(  387):        at com.playfish.our.app.activity.App$1.onJsConfirm(App.java:55)
E/AndroidRuntime(  387):        at android.webkit.CallbackProxy.handleMessage(CallbackProxy.java:526)
E/AndroidRuntime(  387):        at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(  387):        at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime(  387):        at android.app.ActivityThread.main(ActivityThread.java:3683)
E/AndroidRuntime(  387):        at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(  387):        at java.lang.reflect.Method.invoke(Method.java:507)
E/AndroidRuntime(  387):        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
E/AndroidRuntime(  387):        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
E/AndroidRuntime(  387):        at dalvik.system.NativeStart.main(Native Method)
W/ActivityManager(   60):   Force finishing activity com.playfish.our.app/.activity.App
W/ActivityManager(   60): Activity pause timeout for HistoryRecord{4071cfc0 com.playfish.our.app/.activity.App}

我的对话框的代码是:

    webview.setWebChromeClient(new WebChromeClient() {
          public boolean onConsoleMessage(ConsoleMessage cm) {
            Log.d(getClass().getName(), cm.message() + " -- From line "
                    + cm.lineNumber() + " of "
                    + cm.sourceId());
            return true;
        }

          @Override
          public void onReachedMaxAppCacheSize(long spaceNeeded, long totalUsedQuota,    WebStorage.QuotaUpdater quotaUpdater)
          {
                quotaUpdater.updateQuota(spaceNeeded * 2);
          }

         @Override
         public boolean onJsConfirm(WebView view, String url, String message, final JsResult result) {

             new AlertDialog.Builder(App.this)
            .setTitle("Update")
            .setMessage(message)
            .setPositiveButton(android.R.string.yes,
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            result.confirm();
                        }
                    })
            .setNegativeButton(android.R.string.no,
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            result.cancel();
                        }
                    })
            .create()
            .show();

            return true;
        }
     });

代码目前组织得不是很好(或很好),所以请不要纠缠于此;我会在它工作后重构:)

我认为这与传递给对话框的上下文错误有关,我在网上找到了几个类似问题的答案,但没有一个对我有用。我已经为上下文使用了其他值,但它们都不起作用,例如 getApplicationContext()、webview.getContext() 等......任何帮助将不胜感激。

顺便说一句,我很有可能正在做一些非常愚蠢的事情,因为我是一个几乎没有 java 经验的 JS 开发人员。

4

0 回答 0