Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/221.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 如果无法加载网页,如何在webview中显示默认图像_Android_Url_Webview_Android Webview_Internet Connection - Fatal编程技术网

Android 如果无法加载网页,如何在webview中显示默认图像

Android 如果无法加载网页,如何在webview中显示默认图像,android,url,webview,android-webview,internet-connection,Android,Url,Webview,Android Webview,Internet Connection,我在布局中有一个WebView,它基本上是加载给定的url。一切正常。 但是,当网页因某些原因而不可用时,例如如果WebView无法访问服务器或网页被删除,则WebView会显示“网页不可用。www.givenurl_something.com上的网页可能暂时关闭”等。 我希望在WebView中显示应用程序本身中存储的默认图像,而不是向用户显示此文本。 我的意思是当一切正常时,网页应该打开。否则,将显示默认图像,有人可以帮助我吗? 下面是我的代码 public class WebviewActi

我在布局中有一个WebView,它基本上是加载给定的url。一切正常。 但是,当网页因某些原因而不可用时,例如如果WebView无法访问服务器或网页被删除,则WebView会显示“网页不可用。www.givenurl_something.com上的网页可能暂时关闭”等。 我希望在WebView中显示应用程序本身中存储的默认图像,而不是向用户显示此文本。 我的意思是当一切正常时,网页应该打开。否则,将显示默认图像,有人可以帮助我吗? 下面是我的代码

public class WebviewActivity extends Activity {
// flag for Internet connection status
Boolean isInternetPresent = false;

// Connection detector class
ConnectionDetector cd;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    getWindow().requestFeature(Window.FEATURE_PROGRESS);

    setContentView(R.layout.webview);
    cd = new ConnectionDetector(getApplicationContext());

    // Create ad request
    isInternetPresent = cd.isConnectingToInternet();
    if (isInternetPresent) {

        WebView w1 = (WebView) findViewById(R.id.webView1);
        w1.setWebViewClient(new WebViewClient());
        w1.getSettings().setJavaScriptEnabled(true);
        w1.getSettings().setBuiltInZoomControls(true);

        w1.loadUrl("http://www.google.com");
        WebClientClass webViewClient = new WebClientClass();
        w1.setWebViewClient(webViewClient);
        WebChromeClient webChromeClient = new WebChromeClient();
        w1.setWebChromeClient(webChromeClient);
    }

    else {

        showAlertDialog(WebviewActivity.this, "No Internet Connection",
                "You don't have internet connection.", false);
    }
}

public class WebClientClass extends WebViewClient {
    ProgressDialog pd = null;

    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        super.onPageStarted(view, url, favicon);
        pd = new ProgressDialog(WebviewActivity.this);
        pd.setTitle("Please wait");
        pd.setMessage("Page is loading..");
        pd.show();
    }

    @Override
    public void onPageFinished(WebView view, String url) {
        super.onPageFinished(view, url);
        pd.dismiss();
    }

}

public class WebChromeClass extends WebChromeClient {
}

public void showAlertDialog(Context context, String title, String message,
        Boolean status) {
    AlertDialog alertDialog = new AlertDialog.Builder(context).create();

    // Setting Dialog Title
    alertDialog.setTitle(title);

    // Setting Dialog Message
    alertDialog.setMessage(message);

    // Setting alert dialog icon
    // alertDialog.setIcon(R.id.action_mode_close_button);

    // Setting OK Button
    alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            finish();
        }
    });

    // Showing Alert Message
    alertDialog.show();
}

您可以覆盖
onReceivedError(网络视图视图、内部错误代码、字符串描述、字符串失败URL)
WebViewClient类的函数。
在这种情况下,当webview在加载页面时收到任何错误时,您可以显示默认图像。您还可以检查错误代码,以针对不同的故障情况加载不同的映像

有关恒定错误代码的详细信息,请参阅适用于WebViewClient的android文档:

使用此代码覆盖错误页面并加载自己的自定义页面。并将此自定义错误页(myerrorpage.html)和自定义图像保存在资产文件夹中

mWebView.setWebViewClient(new WebViewClient() {
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                mWebView.loadUrl("file:///android_asset/myerrorpage.html");

            }
        });
可能重复的