Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/191.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 7.0尝试在webview中打开url时显示空白屏幕_Android_Webview_Android 7.0 Nougat - Fatal编程技术网

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

Android 7.0尝试在webview中打开url时显示空白屏幕,android,webview,android-7.0-nougat,Android,Webview,Android 7.0 Nougat,我在Android 7.0(moto g4)中遇到了一个问题,我试图在webview中加载一个url,但它显示了一个空白的白色屏幕。它正在使用Android M和更低版本 public class MainActivity extends Activity { private String webviewURL="http://henmilholidayhomesgoa.com/player2/documentation/EULA.html"; private WebView w

我在Android 7.0(moto g4)中遇到了一个问题,我试图在webview中加载一个url,但它显示了一个空白的白色屏幕。它正在使用Android M和更低版本

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不是实际的url,不幸的是我无法共享:(.这是一个带有https的url。但是文章中给定的url实际工作,如果需要任何其他输入,请告诉我。提前感谢

<?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>

尝试以下示例代码:

在这个代码中,有一个webview和一个进度条。在这个代码中,一旦进度条消失,url就会被加载,如果url中有任何链接,它也会在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 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>
导入android.app.Activity;
导入android.app.ProgressDialog;
导入android.os.Bundle;
导入android.webkit.WebView;
导入android.webkit.WebViewClient;
公共类ShowWebView扩展活动{
//私人按钮;
私有网络视图;
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.show\u web\u视图);
//获取网络视图
webView=(webView)findViewById(R.id.webView1);
startWebView(“http://www.androidexample.com/media/webview/login.html");
}
私有void startWebView(字符串url){
//创建新的webview客户端以显示进度对话框
//打开url或单击链接时
setWebViewClient(新的WebViewClient(){
进行对话进行对话;
//如果您不使用此方法,url链接将在新浏览器中打开,而不是在webview中
公共布尔值应重写url加载(WebView视图,字符串url){
view.loadUrl(url);
返回true;
}
//在url加载时显示加载程序
public void onLoadResource(WebView视图,字符串url){
如果(progressDialog==null){
//在标准情况下,您的活动是
progressDialog=新建progressDialog(ShowWebView.this);
progressDialog.setMessage(“加载…”);
progressDialog.show();
}
}
公共void onPageFinished(WebView视图,字符串url){
试一试{
if(progressDialog.isShowing()){
progressDialog.disclose();
progressDialog=null;
}
}捕获(异常){
异常。printStackTrace();
}
}
}); 
//在webview上不使用Javascript
webView.getSettings().setJavaScriptEnabled(true);
//其他网络视图选项
/*
webView.getSettings().setLoadWithOverview模式(true);
webView.getSettings().setUseWideViewPort(true);
webView.setScrollBarStyle(webView.SCROLLBARS\u外部\u覆盖);
webView.SetScrollBarFadinEnabled(假);
webView.getSettings().setBuilTinZoomControl(true);
*/
/*
String summary=“您得了192分。”;
loadData(摘要,“text/html”,空);
*/
//在webview中加载url
loadUrl(url);
}
//按下“后退”按钮时,从webview上的历史记录打开以前打开的链接
@凌驾
//检测何时按下后退按钮
public void onBackPressed(){
if(webView.canGoBack()){
webView.goBack();
}否则{
//让系统处理后退按钮
super.onBackPressed();
}
}
}
下面是相应的xml代码:

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 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>


请在android 7.0中查看此代码。希望它能帮助您!

我复制了您的相同代码,并在nougat 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);
}}


希望这有帮助。愉快的编码。请让我知道它是否有帮助。

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

你可以使用loadDataWithBaseURL而不是loadData,然后再试一次,我在一些开发中遇到了类似的问题ices.

您打开的是哪种类型的url,我的意思是任何文档、网站、pdf或任何其他媒体类型?如果上述url工作正常,那么您所需的url也应该正常。您的帖子中的url有http,请尝试使用https输入其他url,例如:“你需要找到一些可以重现问题的URL。当然,当我找到一个@CommonsWareI时,我会更新它。我将html页面保存到我的资产文件夹中,并尝试使用它(在android 7.0中)但其中显示了一些ASCII字符。因此我尝试通过编程方式检索url内容,并使用webview.loadData(响应,“text/html;charset=utf-8”,null);这也很有效(在android 7.0中).对此有何评论?您好,先生,我使用过Google pixel。我面临着同样的问题,根据您的评论,我已经更新了我的crome应用程序,但仍然存在同样的问题。请回复我任何其他解决方案。提前感谢您。