2016-04-27 58 views
3

我在我的應用中使用Webview來顯示帶有Javascript的網站。我使用此代碼:Webview未顯示(僅適用於Android 6)

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顯示smarphones 6.(其他Android版本都工作正常) 在Android上6,這裏是我的logcat: http://pastebin.com/fh1Aqp0x

一錯誤我不明白的是:

BindingManager的:不能調用determinedVisibility() - 從沒見過的PID的連接:22202

做任何人知道解決辦法?

+0

可能是您在Manifest中錯過的權限,android 6對權限很敏感。 – Petro

回答

-1

我很幸運解決了我的問題。問題是由佈局造成的。上級小部件具有前景色屬性。對於大多數Android版本,它運行良好,但Android 6以不同的方式構建了佈局。現在,當我改變了我的佈局,它正常工作。

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(); 
    } 
} 
} 

希望這適用於你。如果有幫助,請接受答案。歡呼:)

0

使用新棉花糖設備檢查Android System WebView的應用程序更新。 您的logcat第5行表示您的webview版本爲44,當前值爲49時,您的其他設備可能已有。

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" /> 
相關問題