繁体   English   中英

为什么我的 html 文件中的 Javascript 无法在 Android WebVIew 中显示?

[英]Why my Javascript in html file can't display in Android WebVIew?

当我使用 WebView.load URL 加载该 html 文件时,我的 html 文件中的 javascript 代码可以正确运行。 但我想使用 WebView.loadData ,然后我在一个字符串中读取该 html 文件,并使用 WebView.loadData。 html 中的 JavaScript 代码无法成功运行。 谁能帮我? 谢谢。

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    webView = (WebView) findViewById(R.id.webView);
    webView.getSettings().setJavaScriptEnabled(true);
    try {
        InputStream is=this.getAssets().open("COPY-ME.html");
        ByteArrayOutputStream baos=new ByteArrayOutputStream();
        int i=1;
        while ((i=is.read())!=-1) {
            baos.write(i);
        }
        data=baos.toString();

    } catch (IOException e) {
        // TODO 自动生成的 catch 块
        e.printStackTrace();
    }
    webView.loadData(data, "text/html", "utf-8");

尝试使用以下代码加载您的 html 文件

webView.loadUrl("file:///android_asset/COPY-ME.html");

建议将您的资源名称保留为小写字母。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM