brian brian - 4 months ago 42
Android Question

WebView issue on Android

I use below code to read .html file with WebView,

and open hyperlink with other browser app.

webView = (WebView)findViewById(R.id.terms_of_services_webview);
String Path = "..."; //html file path
WebSettings websettings = webView.getSettings();
websettings.setSupportZoom(true);
websettings.setBuiltInZoomControls(true);
websettings.setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(url.equals(Path)) {
view.loadUrl(url);
}
else {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
TermsOfServicesActivity.this.startActivity(intent);
}
return true;
}
});

webView.loadUrl(Path);


The code all are in onCreate method.

But I find blow code on Galaxy Tab 8.9(Android 4.0.4):

11-20 16:24:49.613: W/webview(25896): java.lang.Throwable: Warning: A WebView method was called on thread 'WebViewCoreThread'. All WebView methods must be called on the UI thread. Future versions of WebView may not support use on other threads.
11-20 16:24:49.613: W/webview(25896): at android.webkit.WebView.checkThread(WebView.java:14240)
11-20 16:24:49.613: W/webview(25896): at android.webkit.WebView.getSettings(WebView.java:5374)
11-20 16:24:49.613: W/webview(25896): at android.webkit.WebView.getVisibleTitleHeightImpl(WebView.java:2340)
11-20 16:24:49.613: W/webview(25896): at android.webkit.WebView.getViewHeight(WebView.java:2382)
11-20 16:24:49.613: W/webview(25896): at android.webkit.WebViewCore.setupViewport(WebViewCore.java:3421)
11-20 16:24:49.613: W/webview(25896): at android.webkit.WebViewCore.didFirstLayout(WebViewCore.java:3148)
11-20 16:24:49.613: W/webview(25896): at android.webkit.JWebCoreJavaBridge.nativeServiceFuncPtrQueue(Native Method)
11-20 16:24:49.613: W/webview(25896): at android.webkit.JWebCoreJavaBridge.handleMessage(JWebCoreJavaBridge.java:113)
11-20 16:24:49.613: W/webview(25896): at android.os.Handler.dispatchMessage(Handler.java:99)
11-20 16:24:49.613: W/webview(25896): at android.os.Looper.loop(Looper.java:137)
11-20 16:24:49.613: W/webview(25896): at android.webkit.WebViewCore$WebCoreThread.run(WebViewCore.java:828)
11-20 16:24:49.613: W/webview(25896): at java.lang.Thread.run(Thread.java:856)
11-20 16:24:49.623: E/StrictMode(25896): null
11-20 16:24:49.623: E/StrictMode(25896): java.lang.Throwable: Warning: A WebView method was called on thread 'WebViewCoreThread'. All WebView methods must be called on the UI thread. Future versions of WebView may not support use on other threads.
11-20 16:24:49.623: E/StrictMode(25896): at android.webkit.WebView.checkThread(WebView.java:14240)
11-20 16:24:49.623: E/StrictMode(25896): at android.webkit.WebView.getSettings(WebView.java:5374)
11-20 16:24:49.623: E/StrictMode(25896): at android.webkit.WebView.getVisibleTitleHeightImpl(WebView.java:2340)
11-20 16:24:49.623: E/StrictMode(25896): at android.webkit.WebView.getViewHeight(WebView.java:2382)
11-20 16:24:49.623: E/StrictMode(25896): at android.webkit.WebViewCore.setupViewport(WebViewCore.java:3421)
11-20 16:24:49.623: E/StrictMode(25896): at android.webkit.WebViewCore.didFirstLayout(WebViewCore.java:3148)
11-20 16:24:49.623: E/StrictMode(25896): at android.webkit.JWebCoreJavaBridge.nativeServiceFuncPtrQueue(Native Method)
11-20 16:24:49.623: E/StrictMode(25896): at android.webkit.JWebCoreJavaBridge.handleMessage(JWebCoreJavaBridge.java:113)
11-20 16:24:49.623: E/StrictMode(25896): at android.os.Handler.dispatchMessage(Handler.java:99)
11-20 16:24:49.623: E/StrictMode(25896): at android.os.Looper.loop(Looper.java:137)
11-20 16:24:49.623: E/StrictMode(25896): at android.webkit.WebViewCore$WebCoreThread.run(WebViewCore.java:828)
11-20 16:24:49.623: E/StrictMode(25896): at java.lang.Thread.run(Thread.java:856)


But it works on Xperia z11(Android 4.2.2), Nexus 7 2013(Android 4.3.1), ASUS TF201(Android 4.1.1).

Why it not works on 4.0.3?

How can I avoid it?

Answer

As the warning says you are calling the webview methods in the WebViewCoreThread. Thus modify your code like this,

YourActivity.this.runOnUiThread(new Runnable() {
    public void run() {
       websettings.setSupportZoom(true);  
       websettings.setBuiltInZoomControls(true);   
       websettings.setJavaScriptEnabled(true);  
   webView.setWebViewClient(new WebViewClient() {
  @Override
  public boolean shouldOverrideUrlLoading(WebView view, String url) {
    if(url.equals(Path)) {
        view.loadUrl(url);
    }
    else {
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        TermsOfServicesActivity.this.startActivity(intent);
    }
    return true;  
     }
 });

  webView.loadUrl(Path);  
     }
});

OR

Try to write the below line in your onCreate() method before the setContentView().

  StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);
Comments