gpt4 book ai didi

Android 7.0 尝试在 webview 中打开 url 时显示黑屏

转载 作者:搜寻专家 更新时间:2023-11-01 09:38:28 24 4
gpt4 key购买 nike

我在 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代码:

<?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 中检查此代码。希望对您有所帮助!

关于Android 7.0 尝试在 webview 中打开 url 时显示黑屏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41425766/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com