JavaScript的(存储资产内)不工作内部的WebView在ICS(冰淇淋三明治) [英] JavaScript (stored inside assets) not working inside WebView in ICS (Ice Cream Sandwich)

查看:153
本文介绍了JavaScript的(存储资产内)不工作内部的WebView在ICS(冰淇淋三明治)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  

类似的问题:     JavaScript不上工作ICS

我工作的一个Android应用程序,显示一些code样品给用户。所以,我在一个web视图为语法高亮使用google- code-prettify。但是,问题是,在JS不会对ICS(冰淇淋三明治)单独不起作用。它的工作原理完全可以在所有其他Android版本(2.2 +)的除了4.0.x的。这是我使用的code。

 的WebView web视图=(web视图)findViewById(R.id.webViewSample);
webView.getSettings()setJavaScriptEnabled(真)。
webView.setWebChromeClient(新WebChromeClient());
webView.loadUrl(文件:///android_asset/$c$c_snippets/sample_java.html);
 


  

这是我从logcat中得到的唯一错误样的信息是 UNKNOWN铬   * 错误:-6 的*

任何帮助将是巨大的。先谢谢了。

解决方案

没有找到任何妥善的解决办法。所以,我结束了以下的 MoshErsan 的建议href="http://stackoverflow.com/questions/12550756/javascript-doesnt-work-on-ics">这里:

这是我做的:

 的WebView web视图=(web视图)findViewById(R.id.webView);
webView.setWebViewClient(新WebViewClient(){
    @TargetApi(11)
    @覆盖
    公共WebResourceResponse shouldInterceptRequest(web视图查看,字符串URL){
        Log.d(shouldInterceptRequest,网址);
        InputStream的流= inputStreamForAndroidResource(URL);
        如果(流!= NULL){
            返回新WebResourceResponse(文/ JavaScript的,UTF-8,流);
        }
        返回super.shouldInterceptRequest(查看,网址);
    }
    私人的InputStream inputStreamForAndroidResource(字符串URL){
        最后弦乐ANDROID_ASSET =文件:/// android_asset /;
        如果(url.contains(ANDROID_ASSET)){
            URL = url.replaceFirst(ANDROID_ASSET,);
            尝试 {
                AssetManager资产= getAssets();
                开放的我们的uri = Uri.parse(URL);
                返回assets.open(uri.getPath(),AssetManager.ACCESS_STREAMING);
            }赶上(IOException异常E){
                e.printStackTrace();
            }
        }
        返回null;
    }
});
webView.getSettings()setJavaScriptEnabled(真)。
webView.loadUrl(文件:///android_asset/$c$c_snippets/sample_java.html);
 

不知道实际的问题是什么。但是,这个技巧的工作原理。

Similar Problem : JavaScript doesn't work on ICS

I'm working on an Android app that displays some code samples to the user. So, I'm using google-code-prettify in a WebView for syntax highlighting. But, the problem is, the js does not work on ICS (Ice Cream Sandwich) alone. It works perfectly on all other Android versions (2.2+) except 4.0.x. This is the code that I'm using.

WebView webView = (WebView) findViewById(R.id.webViewSample);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebChromeClient(new WebChromeClient());
webView.loadUrl("file:///android_asset/code_snippets/sample_java.html");


The only error-like message that I get from logcat is UNKNOWN CHROMIUM *ERROR: -6*

Any help would be great. Thanks in advance.

解决方案

Didn't find any proper solution.. So, I ended up following MoshErsan's suggestion as mentioned here:

This is what I did:

WebView webView = (WebView) findViewById(R.id.webView);       
webView.setWebViewClient(new WebViewClient(){
    @TargetApi(11)
    @Override
    public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
        Log.d("shouldInterceptRequest", url);
        InputStream stream = inputStreamForAndroidResource(url);
        if (stream != null) {
            return new WebResourceResponse("text/javascript", "utf-8", stream);
        }
        return super.shouldInterceptRequest(view, url);
    }
    private InputStream inputStreamForAndroidResource(String url) {
        final String ANDROID_ASSET = "file:///android_asset/";
        if (url.contains(ANDROID_ASSET)) {
            url = url.replaceFirst(ANDROID_ASSET, "");
            try {
                AssetManager assets = getAssets();
                Uri uri = Uri.parse(url);
                return assets.open(uri.getPath(), AssetManager.ACCESS_STREAMING);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }           
});
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("file:///android_asset/code_snippets/sample_java.html");

Don't know what the actual problem is. But, this hack works.

这篇关于JavaScript的(存储资产内)不工作内部的WebView在ICS(冰淇淋三明治)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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