当试图在webview中打开url时,Android 7.0显示空白屏幕

问题描述:

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

试试这个示例代码:

在此有一个网页视图和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检查这个代码。希望它能帮助你!

我已经复制了相同的代码,并在牛轧糖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让我知道,如果它是有帮助的。

我在华硕Nexus 7上有类似的问题.Webview版本是56,而Chrome浏览器应用版本较低。更新Google Chrome浏览器应用后,webview开始正常工作。

+0

您好,先生,我使用Google像素。我面临同样的问题,并根据您的评论我已更新我的克罗姆应用程序,但仍然同样的问题面孔。请回复我任何其他解决方案。先谢谢你。 –