Android Bug记录–Error: Webview js调用报错Error calling method on NPObject

itlao6 原创 开发&源码 Android评论666字数 681阅读2分16秒阅读模式

其他项目组的项目需要一个简单h5项目,需要帮忙弄个android壳,就一个页面,只不过需要播放声音,没有其他任何js交互,以后也不会有什么js拓展。于是就简单写了个webview,加了js回调,并封装了声音播放类,在js回调中使用。

public class Javascript {
    
    @JavascriptInterface
    public void voice(final String voiceUrl) {
         Player.getInstance().playUrl(voiceUrl);
    }
}

因为声音播放频率低且短,所以没考虑太多,直接在js中播放声音(代码如上),安装后,第一次声音播放正常,但是发现后面再也无法播放声音了,重启app后又可以播放一次声音。后面在h5中加日志后发现在js调用时报错:Error calling method on NPObject.
看到日志,猜测应该是线程问题了,在js回调中加了线程,果然一切正常:
原文地址:简书ThinkinLiu
个人博客:IT老五文章源自IT老刘-https://itlao6.com/636.html

public class Javascript {
    ExecutorService cachedThreadPool = Executors.newCachedThreadPool();
    @JavascriptInterface
    public void voice(final String voiceUrl) {
        cachedThreadPool.execute(new Runnable() {
            @Override
            public void run() {
                Player.getInstance().playUrl(voiceUrl);
            }
        });
    }
}
文章源自IT老刘-https://itlao6.com/636.html文章源自IT老刘-https://itlao6.com/636.html
weinxin
我的微信公众号
微信扫一扫关注公众号,不定时更新
itlao6
  • 本文由 发表于 2018年 10月 30日 17:44:34
  • 转载请务必保留本文链接:https://itlao6.com/636.html
评论  0  访客  0
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定