简体   繁体   English

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

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

The javascript code in my html file can run correctly when I use WebView.load URL to load that html file.当我使用 WebView.load URL 加载该 html 文件时,我的 html 文件中的 javascript 代码可以正确运行。 But i want to use WebView.loadData ,then i read that html file in a String ,and use WebView.loadData.但我想使用 WebView.loadData ,然后我在一个字符串中读取该 html 文件,并使用 WebView.loadData。 The JavaScript code in html can't run successfully. html 中的 JavaScript 代码无法成功运行。 who can help me?谁能帮我? thanks.谢谢。

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

try to load your html file using below code尝试使用以下代码加载您的 html 文件

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

Its advised to keep your resource name in small letters.建议将您的资源名称保留为小写字母。

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

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