1

In my app i want an error activity "NOT A ERROR HTML PAGE". Which contain refresh button and a layout. I have search for it but i found is just a custom html error page from assets folder.

I have created a some code which is below, i want is that when ever network error occurs it will open an activity like this and user can easily refresh page using button.

I want to make error page like this one..

Error page with refresh button

This is not a html error page. This contains layout with refresh button,

public WebView mWebview ;
@Override
protected void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.map);
    mWebview  = new WebView(this);
    mWebview.getSettings().setJavaScriptEnabled(true); // enable javascript

    mWebview.setWebViewClient(new WebViewClient() {
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {


            // Do Something Here,  I don't know what to do.. :(


        }
    });

    mWebview.loadUrl("google.com");
    setContentView(mWebview);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.option, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle presses on the action bar items
    switch (item.getItemId()) {

        case R.id.refresh:
            startActivity(new Intent(this, CeS1.class));
            finish();
            return true;

        case R.id.about:
            startActivity(new Intent(this, Credits_activity.class));
            finish();
            return true;

        case R.id.gotomain:
            startActivity(new Intent(this, MenuActivity.class));
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}
2
  • 1
    implement the onReceivedError() method shown in your code to implement something like startActivity(new Intent(this, error_activity.class)) and you can even send error information along as you start your error_activity.
    – Molluska
    Aug 22, 2015 at 7:58
  • Can you explain in detail??
    – user4873225
    Aug 22, 2015 at 9:02

3 Answers 3

2

call this in your code

 webview.setWebViewClient(new WebViewClient() {
    public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
 //place you error code here
   }
});
1
0

You can create a layout with 2 layers. One is WebView, another is your error layout. When your WebView encounters network error, you can hide your WebView and show errorView with retry button.

0
0

first, you put your HTML error page on the asset folder, then You need override onReceivedError according to the following code,

private class MyWebViewClient extends WebViewClient {
.
.
.

   // api<23
   @Override
   public void onReceivedError(WebView view, int errorCode, String description, String url) {
        view.stopLoading();
        webview.loadUrl("file:///android_asset/error/error.html");
    }

  // api> 23
  @Override
  @TargetApi(android.os.Build.VERSION_CODES.M) 
  public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
    view.stopLoading();
    webview.loadUrl("file:///android_asset/error/error.html");
//          super.onReceivedError(view, request, error);
  }
.
.
.

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.