Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/184.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将Javascript注入WebView赢得';有时我不工作?_Javascript_Android_Webview - Fatal编程技术网

将Javascript注入WebView赢得';有时我不工作?

将Javascript注入WebView赢得';有时我不工作?,javascript,android,webview,Javascript,Android,Webview,我正在使用WebView播放一些html5动画,这些动画依赖于一些javascript代码。在加载网页之前,我需要将javascript代码注入WebView class AnimWebView extends WebView{ getSettings().setJavaScriptEnabled(true); loadAnimation(String animUrl){ //get javascript code String js_code

我正在使用
WebView
播放一些html5动画,这些动画依赖于一些
javascript
代码。在加载网页之前,我需要将
javascript
代码注入
WebView

class AnimWebView extends WebView{
    getSettings().setJavaScriptEnabled(true);

    loadAnimation(String animUrl){
        //get javascript code
        String js_code = getMyJsCode();
        //inject the javascript code to WebView   
        mWebView.loadUrl("javascript:" + js_code);
        //load animation
        mWebView.loadUrl(animUrl);
    }
}

AnimWebView animContainer = new AnimWebView(context);

// load first animation, the animation works fine.
animContainer.loadAnimation("file:///android_asset/animations/test1/index.html");

//after one minute load second animation(I omit the delay code.)
//it doesn't work because JS code can't be find.
animContainer.loadAnimation("file:///android_asset/animations/test2/index.html");
因为它第一次工作,第二次失败,所以我想第一次之后可能需要清除代码和缓存

所以我试着
clearView()
stopLoading()
loadUrl(“about:blank”)
。但它们都不起作用


有什么建议吗?

您需要添加mWebView.getSettings().setJavascriptEnabled(true)Hi-asgoth,我已经添加了,不起作用。核心问题是webview中的内容第一次运行良好,但第二次失败。