Android WebView显示本地文件的空白页面

时间:2015-04-04 09:13:35

标签: android html webview

当我尝试加载"file:///android_asset/index.html"时,我的Android WebView显示空白页面,并且它完全加载了其他网址"http://twitter.com"

我的活动

public class MainActivity  extends ActionBarActivity  {

    private WebView myWebView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

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

        myWebView = (WebView) findViewById(R.id.webview);

        myWebView.addJavascriptInterface(new WebAppInterface(this), "Android");

        myWebView.setWebViewClient(new WebViewClient());
        WebSettings webSettings = myWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);

        myWebView.loadUrl("file:///android_asset/index.html");
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack()) {
            myWebView.loadUrl("file:///android_asset/index.html");
            return true;
        }
        return false;
    }
}

我的index.html文件:

<!DOCTYPE html>
<html>
<head>
    <title>World Tour</title>
    <!-- Sets initial viewport load and disables zooming  -->
    <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, minimal-ui">

    <!-- Makes your prototype chrome-less once bookmarked to your phone's home screen -->
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="mobile-web-app-capable" content="yes">

    <link rel="stylesheet" type="text/css" href="css/style.css">
    <link rel="stylesheet" type="text/css" href="css/ratchet.css">
</head>
<body>
    <header class="bar bar-footer">
        <a href="views/stats.html" data-transition="slide-out"><span class="icon icon-info pull-left" ></span></a>
        <a href="views/add.html" data-transition="slide-in"><span class="icon icon-plus pull-right" ></span></a>
        <h1 class="title">List of previous drive</h1>
    </header>

    <div class="content">
        <div class="card">
            <ul id="drives-list" class="table-view">
                <li class="table-view-divider table-view-cell" ><p class="pull-left">Comments</p><p class="pull-right">Distance</p></li>
            </ul>
        </div>
    </div>

    <script type="text/javascript" src="js/main.js" ></script>
    <script type="text/javascript" src="js/ratchet.js" ></script>
</body>
</html>

感谢您的帮助!

2 个答案:

答案 0 :(得分:0)

使用以下代码

从资源文件中获取字符串内容
    public static String getStringFromAssets(String name, Context p_context)
    {
        try
        {
            InputStream is = p_context.getAssets().open(name);
            int size = is.available();
            byte[] buffer = new byte[size];
            is.read(buffer);
            is.close();
            String bufferString = new String(buffer);
            return bufferString;
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        return null;

    }

并使用以下代码加载内容

String m_data = getStringFromAssets("index.html", this);
webView.loadDataWithBaseURL("file:///android_asset/", m_data, "text/html", "utf-8", null);

希望它适合你!!

答案 1 :(得分:0)

检查与html页面关联的javascript代码中是否存在任何错误。如果是这样,android studio不会指出js文件中的错误。

我修复了js文件中的错误并且它有效。