我用下面的代码用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)上找到了blow代码:
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 2013(android4.3.1)、asustf01(android4.1.1)上运行。
为什么它不能在4.0.3上工作?
我怎样才能避免呢?
最佳答案
如警告所示,您正在调用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);
}
});
或
尝试在
onCreate()
之前用setContentView()
方法写下一行。 StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);