我使用下面的代码用WebView读取.html文件,
和其他浏览器应用程序打开超链接。
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);
所有代码都在onCreate方法中。
但我在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)
但它适用于 Xperia z11(Android 4.2.2), Nexus 7 2013(Android 4.3.1),华硕TF201(Android 4.1.1) )。
为什么它不适用于4.0.3?
我怎么能避免呢?
答案 0 :(得分:4)
正如警告所示,您正在调用WebViewCoreThread
中的webview方法。因此,修改你的代码,
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
尝试在onCreate()
之前的setContentView()
方法中写下以下一行。
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
答案 1 :(得分:0)
您需要在UI线程中使用WebView。为此,您可以使用Runnable:
(