一般来说,可能第一时间想到的是自定义一个html来替代webview内置的异常页面。  但是实际操作时,这种方法比较困难。

这里介绍一个简单的替代方案,希望能有所帮助。

可以采用嵌套layout的方式,然后在webview的错WebViewClient的onReceivedError方法中控制异视图的显示和隐藏,具体代码如下:

public class DefaultWebViewClient extends WebViewClient {
boolean isInErrorState = false;
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
//这里可以将异常信息也显示出来
isInErrorState = true;
mEmptyView.setVisibility(View.VISIBLE);
webView.setVisibility(View.GONE);
mEmptyViewIndicator.setImageResource(R.drawable.no_service);
mEmptyView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
isInErrorState = false;
webView.reload();
}
});
// super.onReceivedError(view, errorCode, description, failingUrl);
} @Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
if(!isInErrorState) {
mEmptyView.setVisibility(View.GONE);
webView.setVisibility(View.VISIBLE);
}else {
mEmptyView.setVisibility(View.VISIBLE);
webView.setVisibility(View.GONE);
}
super.onPageStarted(view, url, favicon);
}
}

对应的layout如下:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:orientation="vertical"

android:layout_width="match_parent"

android:layout_height="match_parent">

<WebView

android:id="@+id/webView"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:layout_gravity="center_horizontal" />

<LinearLayout

android:orientation="vertical" android:layout_width="match_parent"

android:id="@+id/empty_view_root"

android:layout_height="match_parent"

android:gravity="center"

android:paddingTop="50dp"

android:paddingBottom="50dp">

<ImageView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:id="@+id/empty_view_indicator"

android:layout_gravity="center"

android:src="@drawable/pic_nothing" />

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:id="@+id/empty_view_text"

android:textColor="#999999"

android:singleLine="false" />

</LinearLayout>

</RelativeLayout>

如何实现自定义的android WebView错误页的更多相关文章

  1. cPanel设置自定义404错误页

    利用这个cpanel的错误页工具,你就可以定制错误页面了.设置自定义404错误页,有两种简单的方法. 一,利用cpanel后台控制面板添加设置404自定义错误页的方法 步骤       1.登录cPa ...

  2. 在Asp.Net的Global.asax中Application_Error跳转到自定义错误页无效的解决办法

    在开发Asp.Net系统的时候,我们很多时候希望系统发生错误后能够跳转到一个自定义的错误页面,于是我们经常会在Global.asax中的Application_Error方法中使用Response.R ...

  3. MVC自定义错误页404静态页

    昨天公司要求给所有项目添加自定义404错误页,具体的要求实现的有以下几点: 1.实现自定义错误(如各种error,404等)跳转到指定的页面 2.所指定的页面输出的http状态值必须是404或其他指定 ...

  4. ASP.NET MVC下自定义错误页和展示错误页的几种方式

    在网站运行中,错误是不可避免的,错误页的产生也是不可缺少的. 这几天看了博友的很多文章,自己想总结下我从中学到的和实际中配置的. 首先,需要知道产生错误页的来源,一种是我们的.NET平台抛出的,一种是 ...

  5. ASP.NET MVC-异常处理&自定义错误页

    一.应用场景 对于B/S应用程序,在部署到正式环境运行的过程中,很有可能出现一些在前期测试过程中没有发现的一些异常或者错误,或者说只有在特定条件满足时才会发生的一些异常,对于使用ASP.NET MVC ...

  6. 解决 ASP.NET Core 自定义错误页面对 Middleware 异常无效的问题

    我们基于 Razor Class Library 实现了自定义错误页面的公用类库(详见之前的随笔),但是在实际使用时发现如果在 middleware 中发生了异常,则不能显示自定义错误页面,而是返回默 ...

  7. MVC 实现自定义404错误页

    直接进入正题. 在HomeController中有一个NotFound的Action方法. public ActionResult NotFound() { return View(); } 对应的视 ...

  8. MVC3 自定义的错误页

    ASP.NET MVC3中如果配置文件出错了,怎么跳转到自定义的错误页,现在参考网上的档案是说 添加 如下配置文件,并且在路径Views/Shared/下添加Error页面,测试下没有用的,请大家看看 ...

  9. 关于在 ASP.NET 的 Global.asax 中 Application_Error 方法内,设置跳转到自定义错误页无效的问题

    转自:https://www.cnblogs.com/OpenCoder/p/5070645.html 在 Global.asax 中的 Application_Error 方法中,使用 Respon ...

随机推荐

  1. U-Boot移植

    基于天翔的老师的课程, 他的博客在这儿: http://blog.csdn.net/johnmcu/article/details/6561311 注明不能转载, 就重新写一下吧: 1. 安装韦东山的 ...

  2. openWrt 安装与实践

    1. 先装一个编译用的环境, ubuntu 14 2. 在ubuntu里面安装svn, g++, libncurses5-dev git libssl-dev gawk, svn因为openwrt社区 ...

  3. 【转】Tomcat版本是32位、64位问题

    转载地址:http://www.cnblogs.com/greensleeves/p/3168541.html 最近遇到一个Tomcat windows安装版本是32位还是64位问题.由于一系列原因, ...

  4. Aspnetpage ie10下 __dopost方法未找到 不能翻页的问题

    1.问题分析: 没有__dopost 的原因是因为没有 ie10下 页面里 没有这个 方法,和 2个 input 标签,ie10 没有解析出来,所以就不能翻页了. 2.解决办法:(缺什么补什么,将这个 ...

  5. js计算两个日期的差值

    // 获取两个比较值的毫秒数var postman_confirmtime_ms = Date.parse(new Date(data.postman_confirmtime.replace(/-/g ...

  6. AutoMagic

    AutoMagic 是一个基于WebUI的自动化管理平台.为什么叫AutoMagic呢?因为自动化(Automation)在执行起来的时候是一个很神奇的事情,它可以无人值守的模拟人的操作,就像魔术(M ...

  7. [转载]Back up all of your mysql databases nightly

    原文地址:http://www.linuxbrigade.com/back-up-all-of-your-mysql-databases-nightly/ Put the following into ...

  8. 使用Xmanager远程访问Redhat Enterprise Linux 6.1

    使用Xmanager远程访问Redhat Enterprise Linux 6.1   在Linux服务器开启gdm 配置防火墙 配置selinux 使用xmanager连接linux服务器 在Lin ...

  9. kegg-kass注释--转载

    在注释KEGG的时候,一直用到kaas,具体kaas是个什么东东,简单的总结一下吧.     KEGG是由日本人搞的一个代谢图,收录基因和基因组的数据库,数据库可以分为 3大部分,基因数据库, 化学分 ...

  10. 单例模式getInstance()

    /** * 对象的实例化方法,也是比较多的,最常用的方法是直接使用new,而这是最普通的,如果要考虑到其它的需要,如单实例模式,层次间调用等等. * 直接使用new就不可以实现好的设计好,这时候需要使 ...