应用程序崩溃的科尔多瓦警报 [英] App crashes on Cordova alert

查看:75
本文介绍了应用程序崩溃的科尔多瓦警报的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在某些Android设备,我身边的时候,基于科尔多瓦,应用程序崩溃我提示用户,如果他们想收到通知,这是发生在第一个应用程序启动。下面是一个典型堆栈跟踪:

On some Android devices, my Cordova-based app crashes around the time I prompt the user if they'd like to receive notifications, which happens on the first app startup. Here's a typical stack trace:

android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@<i>[eight hex digits]</i> is not valid; is your activity running?
at android.view.ViewRoot.setView(ViewRoot.java:535)
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.AlertDialog$Builder.show(AlertDialog.java:802)
at org.apache.cordova.Notification$2.run(Notification.java:245)
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:3806)
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:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)

JavaScript的code:

The JavaScript code:

navigator.notification.confirm(
     'Do you wish to receive push notifications?',
     function(btnIndex) {
         if (btnIndex == 1) {
             push.enablePush();
             localStorage.pushAsked = true;
         } else {
             push.disablePush();
             localStorage.pushAsked = true;
         }
     },
     'Push Notifications',
     'Yes,No'
);

任何想法?我不知道哪些设备或Android版本会受到影响,但问题是,在那里那些谁报告了。

Any ideas? I don't know which devices or Android versions are affected, but that question is out there to those who have reported it.

我使用的科尔多瓦2.2.0。

I'm using Cordova 2.2.0.

推荐答案

这是因为你试图同时活动正在整理显示警告对话框。

This is because you are trying to display alert dialog while the activity is finishing.

您可以继承CordovaChromeClient和检查activity.isFinishing()上onJsAlert()

You can subclass CordovaChromeClient and check for activity.isFinishing() on onJsAlert()

我已经建立在Github上的一个项目,解决了这个错误:
https://github.com/kruyvanna/CordovaAlertBug_Android

I have created a project on Github that solves this bug: https://github.com/kruyvanna/CordovaAlertBug_Android

您可以看到下面的例子。

You can see an example below.

class CordovaOnJsAlertBug extends DroidGap{
@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    super.loadUrl("file:///android_asset/www/index.html");
}

@Override
public void init() {
    Log.e(TAG, "init()");
    CordovaWebView webView = new CordovaWebView(CordovaOnJsAlertBug.this);
    CordovaWebViewClient webViewClient;
    if(android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.HONEYCOMB)
    {
        webViewClient = new CordovaWebViewClient(this, webView);
    }
    else
    {
        webViewClient = new IceCreamCordovaWebViewClient(this, webView);
    }
    this.init(webView, webViewClient, new MyCordovaChromeClient(this, webView));
}

private class MyCordovaChromeClient extends CordovaChromeClient{
    private CordovaInterface cordova;
    public MyCordovaChromeClient(CordovaInterface ctx, CordovaWebView app) {
        super(ctx, app);
        this.cordova = ctx;
    }


    @Override
    public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
        if(cordova.getActivity().isFinishing()){
            Log.w(TAG, "Trying to alert while activity is finishing!! -> ignore");
            result.cancel();
            return true;
        }

        return super.onJsAlert(view, url, message, result);
    }
}
}

这篇关于应用程序崩溃的科尔多瓦警报的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆