Android:WebView加載url網頁顯示不完整解決辦法

WebView基本用法

如果想要在APP裏面加載url網頁,或者html代碼,首先我們會想到WebView,它的基本用法如下:
webview_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:orientation="vertical" >

    <WebView
        android:id="@+id/survey_webview"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

MainActivity.java

WebView mWebView= (WebView) this.findViewById(R.id.survey_webview);
// 設置支持js,默認爲false
// mWebView.getSettings().setJavaScriptEnabled(true);
// 設置緩存模式:不使用緩存
// mWebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
// 加載指定url鏈接
mWebView.loadUrl(URL);
// 加載本地html代碼可以使用如下方法
// mWebView.public void loadDataWithBaseURL(String baseUrl, String data, String mimeType,  String encoding, String historyUrl);
// 舉個栗子:
// mWebView.loadDataWithBaseURL(null, htmlData, "txt/html", "utf-8", null);
mWeb.setWebViewClient(new WebViewClient() {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        // 在APP內部打開鏈接,不要調用系統瀏覽器
        view.loadUrl(url);
        return true;
    }
});

使用以上代碼基本可以實現一個網頁的加載了。

攤上大事了

BUT!!!爲什麼我的網頁只加載出一部分,下面就不加載了?今天就要發版本了有木有??
多謝logcat小貓幫我捕捉到一條log:

  • I/chromium(27693): [INFO:CONSOLE(8)] “Uncaught TypeError: Cannot call method ‘getItem’ of null”, source: url

在stackoverflow大神的幫助下解決該問題,網頁加載不完成並報出如上錯誤時,有可能是你的DOM儲存API沒有打開,在代碼中加上一行:

mWebView.getSettings().setDomStorageEnabled(true);

網頁華麗麗的加載出來了,希望本文對大家有所幫助。

本文作者xiong_it,本文鏈接:http://blog.csdn.net/Xiong_IT/article/details/50549340

參考鏈接
http://stackoverflow.com/questions/4930623/webview-causing-uncaught-typeerror-when-loading-www-google-com

發佈了69 篇原創文章 · 獲贊 251 · 訪問量 49萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章