2017-01-02 129 views
2

Im在Android 7.0(moto g4)中,我試圖在webview中加載一個url,但它顯示一個空白的白屏screen.It與Android M和下面。當試圖在webview中打開url時,Android 7.0顯示空白屏幕

public class MainActivity extends Activity { 

private String webviewURL="http://henmilholidayhomesgoa.com/player2/documentation/EULA.html"; 


    private WebView wv; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     wv= (WebView) findViewById(R.id.webview); 
     wv.getSettings().setTextZoom(60); 
     wv.getSettings().setBuiltInZoomControls(true); 
     wv.getSettings().setJavaScriptEnabled(true); 
     wv.loadUrl(webviewURL); 

    } 

} 

注: - 在代碼中提到的網址並非不幸我不能分享:(實際的URL及其與HTTPS URL的,但在後給定的URL實際工作,請讓我知道。如果任何其它的輸入是事先needed.Thanks

<?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" > 
      <WebView 
       android:id="@+id/webview" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:gravity="center" /> 
    </RelativeLayout> 
+1

你打開什麼類型的網址,我的意思是說任何文件或網站或PDF或任何其他媒體類型? –

+1

如果上面的網址可以正常工作,那麼應該使用你想要的網址。 在您的文章中的網址有http嘗試輸入其他網址與https 說:「https://www.google.com」 –

+1

您將需要找到一些URL,可以重現問題,你*可以*把在你的問題。 – CommonsWare

回答

1

試試這個示例代碼:

在此有一個網頁視圖和bar.In該代碼的URL進度將作爲進度條快裝消失,如果有任何鏈接w在網址中,它將在webview中打開它。

下面是的Java代碼

import android.app.Activity; 
import android.app.ProgressDialog; 
import android.os.Bundle; 
import android.webkit.WebView; 
import android.webkit.WebViewClient; 

public class ShowWebView extends Activity { 

    //private Button button; 
    private WebView webView; 
    public void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 
     setContentView(R.layout.show_web_view); 

     //Get webview 
     webView = (WebView) findViewById(R.id.webView1); 

     startWebView("http://www.androidexample.com/media/webview/login.html"); 

    } 

    private void startWebView(String url) { 

     //Create new webview Client to show progress dialog 
     //When opening a url or click on link 

     webView.setWebViewClient(new WebViewClient() {  
      ProgressDialog progressDialog; 

      //If you will not use this method url links are opeen in new brower not in webview 
      public boolean shouldOverrideUrlLoading(WebView view, String url) {    
       view.loadUrl(url); 
       return true; 
      } 

      //Show loader on url load 
      public void onLoadResource (WebView view, String url) { 
       if (progressDialog == null) { 
        // in standard case YourActivity.this 
        progressDialog = new ProgressDialog(ShowWebView.this); 
        progressDialog.setMessage("Loading..."); 
        progressDialog.show(); 
       } 
      } 
      public void onPageFinished(WebView view, String url) { 
       try{ 
       if (progressDialog.isShowing()) { 
        progressDialog.dismiss(); 
        progressDialog = null; 
       } 
       }catch(Exception exception){ 
        exception.printStackTrace(); 
       } 
      } 

     }); 

     // Javascript inabled on webview 
     webView.getSettings().setJavaScriptEnabled(true); 

     // Other webview options 
     /* 
     webView.getSettings().setLoadWithOverviewMode(true); 
     webView.getSettings().setUseWideViewPort(true); 
     webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY); 
     webView.setScrollbarFadingEnabled(false); 
     webView.getSettings().setBuiltInZoomControls(true); 
     */ 

     /* 
     String summary = "<html><body>You scored <b>192</b> points.</body></html>"; 
     webview.loadData(summary, "text/html", null); 
     */ 

     //Load url in webview 
     webView.loadUrl(url); 


    } 

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

} 

現在下面是各自XML代碼:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

    <WebView 
    android:id="@+id/webView1" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
/>  
</LinearLayout> 

請安卓7.0檢查這個代碼。希望它能幫助你!

1

我已經複製了相同的代碼,並在牛軋糖Nexus 9上運行應用程序。 加載URL等待5分鐘需要時間,否則請與其他牛軋糖設備一起檢查。

public class WebviewMainActivity extends AppCompatActivity { 
private WebView wv; 
private String webviewURL = "http://stackoverflow.com/questions/41425766/android-7-0-shows-blank-screen-when-trying-to-open-url-in-webview"; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_webview_main); 
    wv = (WebView) findViewById(R.id.webview); 
    wv.getSettings().setTextZoom(60); 
    wv.getSettings().setBuiltInZoomControls(true); 
    wv.getSettings().setJavaScriptEnabled(true); 
    wv.loadUrl(webviewURL); 
}} 

See this image from Nexus 9

希望這help.Happy coding.Please讓我知道,如果它是有幫助的。

0

我在華碩Nexus 7上有類似的問題.Webview版本是56,而Chrome瀏覽器應用版本較低。更新Google Chrome瀏覽器應用後,webview開始正常工作。

+0

您好,先生,我使用Google像素。我面臨同樣的問題,並根據您的評論我已更新我的克羅姆應用程序,但仍然同樣的問題面孔。請回復我任何其他解決方案。先謝謝你。 –