2013-01-19 52 views
6

我使用webVIew讀取本地HTML。 HTML存儲位置 項目資產android webview錯誤找不到頁面

有些手機可以成功地使用(三星...) 有些手機不能(HTC的Nexus ...)

這裏是我的代碼

public class MainActivity extends Activity { 
    private WebView wvBrowser; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     findViews(); 
    } 

    public boolean onKeyDown(int keyCode, KeyEvent event) { 
     if (wvBrowser.canGoBack() && event.getKeyCode() == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) { 
      wvBrowser.goBack(); 
      return true; 
     } 

     return super.onKeyDown(keyCode, event); 
    } 

    private void findViews() { 
     wvBrowser = (WebView) findViewById(R.id.Browser); 

     //wvBrowser.loadUrl(getString(R.string.googleUrl)); 

     wvBrowser.getSettings().setSupportZoom(true); 
     wvBrowser.getSettings().setBuiltInZoomControls(true); 
     wvBrowser.loadUrl("file:///android_asset/ts.htm");  
    } 
} 
+0

具有u檢查我的答案 –

回答

2

這是你的解決方案

根據文檔和我的經驗,它應該工作得很好。您只需設置您的WebClient重寫方法onReceivedError在您的WebView

這裏是我的一些舊的測試應用程序的代碼片段:

wvBrowser = (WebView) findViewById(R.id.Browser); 
wvBrowser.setWebViewClient(new WebViewClient() { 
    @Override 
    public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { 
      Log.i("WEB_VIEW_TEST", "error code:" + errorCode); 
      super.onReceivedError(view, errorCode, description, failingUrl); 
    } 
}); 

我測試過它和它的作品相當精細。檢查你的日誌,看看你得到什麼樣的代碼錯誤。

這都可以在http://developer.android.com/reference/android/webkit/WebView.html

發現希望它能幫助。

+5

尼斯複製粘貼... http://stackoverflow.com/a/4997988/1003325 – KIDdAe

+0

不固定的工作類似的錯誤,請測試它。 – Pinakin

3

試試這個鏈接它會幫助你更好地webview

附上WebViewClient到您的WebView,在那裏你重寫onReceivedError()

webview.setWebViewClient(new WebViewClient() { 
     public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { 

     } 
    }); 
6

只是想增加一個答案。有人可能會發現我的建議很有幫助。 您需要在AndroidManifest.xml中添加uses-permission。我加入這行

<uses-permission android:name="android.permission.INTERNET" />