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

版权声明:本文为博主原创文章,未经博主允许不得转载商用。 https://blog.csdn.net/andy_5826_liu/article/details/84144262

其他项目组的项目需要一个简单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老五

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

猜你喜欢

转载自blog.csdn.net/andy_5826_liu/article/details/84144262