简体   繁体   English

使用WebView加载本地.HTML文件

[英]Loading local .HTML-file with WebView

I've read many descriptions on how to make this work, but it doesn't in my emulator, which is really frustrating. 我已经阅读了很多关于如何使这项工作的描述,但它并没有在我的模拟器中,这真的令人沮丧。 Should be piece of cake! 应该是小菜一碟!

I made a supersimple html-file: 我制作了一个超级简单的html文件:

 <html>

    <head>
<h1> Hello </h1>

    </head>

    <body>
    </body>


</html>

And then I use this java code to implement it: 然后我使用这个java代码来实现它:

    package com.path.path;

import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;



public class hello extends Activity {

WebView webView;

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.hello);



     String url =  "file:///assets/hello.html";
    webView = (WebView) this.findViewById(R.id.char_view);
    webView.loadUrl(url);


}

} }

When I load this page in the emulator it says the page is not available. 当我在模拟器中加载此页面时,它表示该页面不可用。 What am I missing?? 我错过了什么?

Thanks! 谢谢!

Change file:///assets/hello.html to file:///android_asset/hello.html . file:///assets/hello.html更改为file:///android_asset/hello.html This assumes that your HTML file is located at assets/hello.html in your project. 这假设您的HTML文件位于项目中的assets/hello.html

Also, move your <h1> element into the <body> , so that it will actually work. 此外,将<h1>元素移动到<body> ,以便它实际工作。

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

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