gpt4 book ai didi

android - 检测 Webview 错误并显示消息

转载 作者:IT老高 更新时间:2023-10-28 21:50:21 24 4
gpt4 key购买 nike

我想在加载 web View 页面时显示错误消息(无连接)。这是我目前所拥有的,没有错误处理代码:

public class TrackerPage extends Activity {

// @Override
private WebView webview;
private ProgressDialog progressDialog;

private boolean error;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

// Get rid of the android title bar
requestWindowFeature(Window.FEATURE_NO_TITLE);

// Set the XML layout
setContentView(R.layout.tracker_page);

// Bundle objectbundle = this.getIntent().getExtras();
webview = (WebView) findViewById(R.id.tracker);

final Activity activity = this;

// Enable JavaScript and lets the browser go back
webview.getSettings().setJavaScriptEnabled(true);
webview.canGoBack();

webview.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}

public void onLoadResource(WebView view, String url) {
// Check to see if there is a progress dialog
if (progressDialog == null) {
// If no progress dialog, make one and set message
progressDialog = new ProgressDialog(activity);
progressDialog.setMessage("Loading please wait...");
progressDialog.show();

// Hide the webview while loading
webview.setEnabled(false);
}
}

public void onPageFinished(WebView view, String url) {
// Page is done loading;
// hide the progress dialog and show the webview
if (progressDialog.isShowing()) {
progressDialog.dismiss();
progressDialog = null;
webview.setEnabled(true);
}
}

});

// The URL that webview is loading
webview.loadUrl("http://url.org/");
}
}

我该怎么做?

最佳答案

以上所有答案均已弃用。您应该在页面完成后使用此代码

 @Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error){
//Your code to do
Toast.makeText(getActivity(), "Your Internet Connection May not be active Or " + error.getDescription(), Toast.LENGTH_LONG).show();
}

关于android - 检测 Webview 错误并显示消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6392318/

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