我在我的应用程序中使用Webview来显示带有Javascript的网站。我正在使用此代码:
WebView webview = (WebView) findViewById(R.id.webView);
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setLoadWithOverviewMode(true);
webview.getSettings().setUseWideViewPort(true);
webview.setWebViewClient(new WebViewClient(){
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
@Override
public void onPageFinished(WebView view, final String url) {
}
});
ConnectivityManager connMgr = (ConnectivityManager)
getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isConnected()) {
webview.loadUrl("http://www.example.com");
} else {
summary = "<html><body>No internet connection!</body></html>";
webview.loadData(summary, "text/html", null);
}
但不幸的是,在Android 6的smarphones上没有显示Webview。(其他Android版本工作正常) 在Android 6上这里是我的LogCat: http://pastebin.com/fh1Aqp0x
我不明白的一个错误是:
BindingManager:无法调用determinVisibility() - 从未看到pid的连接:22202
有人知道解决方案吗?
答案 0 :(得分:0)
试试这个希望它有效。它适用于Android 6.0
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class Book extends AppCompatActivity {
//private Button button;
WebView webView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_book);
//Get webview
webView = (WebView) findViewById(R.id.webView1);
runOnUiThread(new Runnable() {
@Override
public void run() {
webView.setWebViewClient(new myWebClient());
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("file:///android_asset/pa.html");
}
});
}
class myWebClient extends WebViewClient
{
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
// Open previous opened link from history on webview when back button pressed
@Override
// Detect when the back button is pressed
public void onBackPressed() {
if(webView.canGoBack()) {
webView.goBack();
} else {
// Let the system handle the back button
super.onBackPressed();
}
}
}
希望这适合你。如果这有帮助,请接受答案。干杯:)
答案 1 :(得分:0)
使用新的Marshmallow设备检查Android System WebView的应用更新。 您的logcat第5行表示您当前的电流为49时有webview版本44,这是您的其他设备可能已经拥有的。
答案 2 :(得分:0)
您可以创建WebViewActivity类
public class WebViewActivity extends AppCompatActivity {
private WebView mWebview;
private ProgressBar progressBar;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_webview);
//pass url using intent;
String url = getIntent().getStringExtra("url");//it's get the url
mWebview = (WebView) findViewById(R.id.webView);
progressBar = (ProgressBar) findViewById(R.id.progressBarWebView);
mWebview.setVisibility(View.INVISIBLE);
mWebview.getSettings().setBuiltInZoomControls(true); // enable zoom in and out
mWebview.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
mWebview.setWebViewClient(new WebViewClient() {
//
public boolean shuldOverrideKeyEvent(WebView view, KeyEvent event) {
// Do something with the event here
return true;
}
public void onPageFinished(WebView view, String url) {
progressBar.setVisibility(View.GONE);
mWebview.setVisibility(View.VISIBLE);
}
public boolean shouldOverrideUrlLoading(WebView view, String url) {
String hostName = Uri.parse(url).getHost();
Log.v("Debug", "Inside override -- url host:" + hostName);
if (hostName.equals("https://example.com/risks")) {
// This is my web site, so do not override; let my WebView load the page
return false;
}
// reject anything other
progressBar.setVisibility(View.GONE);
mWebview.setVisibility(View.VISIBLE);
return true;
}
});
mWebview.loadUrl(url);
}
}
<强> layout_webview.xml 强>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:orientation="vertical">
<ProgressBar
android:id="@+id/progressBarWebView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
<WebView
android:id="@+id/webView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center" />
</RelativeLayout>
AndroidManifest.xml 不要忘记添加此权限
<uses-permission android:name="android.permission.INTERNET" />
答案 3 :(得分:-1)
我幸运地解决了我的问题。问题是由布局引起的。高级小部件具有前景色属性。大多数Android版本都运行良好,但Android 6以不同的方式解释了布局。现在,当我改变布局时,它可以正常工作。