Web视图onReceivedError已处理,但仍显示网页不可用 [英] Web view onReceivedError is handled but still showing web page not available

查看:76
本文介绍了Web视图onReceivedError已处理,但仍显示网页不可用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在我的网络视图中显示一个网页.并且如果发生错误或页面加载失败,我想显示一个悲伤的笑脸,并显示一条文本,表明您的页面未加载或类似的内容.为此,我尝试将代码放入 onReceivedError()内,但除了吐司和日志外,它不知为何不起作用.

I was trying to show a web page on my web view. and in case if an error occurs or page loading fails I want to show a sad smiley and a text showing that your page is not loaded or anything like that. and for that I tried to put my code inside onReceivedError() but somehow it does not work except toasts and logs.

我的代码如下:

webView.setWebViewClient(new WebViewClient(){

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
                actionBar.setTitle(Constants.TITLE_VERIFYING);
                hideError();
                showProgress();
                Toast.makeText(WebViewActivity.this, "start loading", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                actionBar.setTitle(Constants.TITLE_VERIFIED);
                hideError();
                hideProgress();
                Toast.makeText(WebViewActivity.this, "Web view jas loaded", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {


                hideProgress();
                showError();
                Toast.makeText(WebViewActivity.this, "Could not load your page", Toast.LENGTH_SHORT).show();
                super.onReceivedError(view,errorCode,description,failingUrl);
                Toast.makeText(WebViewActivity.this, "error", Toast.LENGTH_SHORT).show();
            }
        });

其他方法是:

private void showProgress() {

    avLoadingIndicatorView.setVisibility(View.VISIBLE);
        loadingText.setVisibility(View.VISIBLE);
        avLoadingIndicatorView.show();
        webView.setAlpha(Constants.ALPHA_LAYOUT);
    }

    private void hideProgress() {
        avLoadingIndicatorView.hide();
        webView.setAlpha(1f);
        loadingText.setVisibility(View.GONE);
        avLoadingIndicatorView.setVisibility(View.GONE);
    }

    private void showError(){
        webView.setVisibility(View.GONE);
        sadSmiley.setVisibility(View.VISIBLE);
        errorText.setVisibility(View.VISIBLE);
    }

    private void hideError(){
        webView.setVisibility(View.VISIBLE);
        sadSmiley.setVisibility(View.GONE);
        errorText.setVisibility(View.GONE);
    }

布局代码:

<android.support.constraint.ConstraintLayout 

xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".activity.WebViewActivity">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/primary"
        app:titleTextColor="@color/white"
        android:minHeight="?attr/actionBarSize" />

    <WebView
        android:id="@+id/web_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="?attr/actionBarSize"
        />

    <com.wang.avi.AVLoadingIndicatorView
        android:id="@+id/loading"
        style="AVLoadingIndicatorView.Small"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:padding="10dp"
        android:elevation="10dp"
        app:indicatorColor="@color/progress"
        app:indicatorName="BallScaleIndicator"
        android:visibility="gone"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"/>

    <TextView
        android:id="@+id/loading_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        style="@style/tvDarkLargeStyle"
        android:text="Verifying Your Document"
        android:visibility="gone"
        android:textColor="@color/primary"
        app:layout_constraintTop_toBottomOf="@+id/loading"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"/>

    <ImageView
        android:id="@+id/img_sad_smiley"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/sad_smiley_primary_64"
        android:layout_margin="10dp"
        android:padding="10dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        android:visibility="gone"/>

    <TextView
        android:id="@+id/error_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        style="@style/tvDarkLargeStyle"
        android:text="Sorry! we could not load your page!"
        android:textColor="@color/primary"
        app:layout_constraintTop_toBottomOf="@+id/img_sad_smiley"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        android:textAlignment="center"
        android:layout_margin="10dp"
        android:visibility="gone"
        />
</android.support.constraint.ConstraintLayout>

请帮助我.谢谢.

推荐答案

由于在 onPageFinished 之前调用了 onReceivedError ,因此您的 sadSmiley errorText 不见了.

Because onReceivedError called before onPageFinished, so your sadSmiley and errorText gone.

尝试下面的代码,

boolean errorOccurred = false; // Global variable

webView.setWebViewClient(new WebViewClient() {
    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        super.onPageStarted(view, url, favicon);
        hideError();
        showProgress();
        Toast.makeText(Test.this, "start loading", Toast.LENGTH_SHORT).show();
        errorOccurred=false;
    }
    @Override
    public void onPageFinished(WebView view, String url) {
        super.onPageFinished(view, url);
        if (!errorOccurred) {
            hideError();
        }
        hideProgress();
        Toast.makeText(Test.this, "Web view was loaded", Toast.LENGTH_SHORT).show();
    }
    @Override
    public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
        errorOccurred = true;
        hideProgress();
        showError();
        Toast.makeText(Test.this, "Could not load your page", Toast.LENGTH_SHORT).show();
        super.onReceivedError(view, errorCode, description, failingUrl);
        Toast.makeText(Test.this, "error", Toast.LENGTH_SHORT).show();
    }
});

这篇关于Web视图onReceivedError已处理,但仍显示网页不可用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆