我想知道当我把这段代码添加到我的代码中时有什么问题。

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

Progressbar加载完成后,textview不会变为不可见
public class YazdMap extends ActionBarActivity {

WebView web;
ProgressBar progressBar;
TextView txt;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_yazd_map);

    web = (WebView) findViewById(R.id.webview1);
    progressBar = (ProgressBar) findViewById(R.id.progressBar);
    txt = (TextView) findViewById(R.id.txt);



    web.setWebViewClient(new myWebClient());
    web.getSettings().setJavaScriptEnabled(true);
    web.loadUrl("http://google.com/");



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


}

public class myWebClient extends WebViewClient
{

    @Override
    public void onPageFinished(WebView view, String url) {
        super.onPageFinished(view, url);
        progressBar.setVisibility(view.GONE);
        txt.setVisibility(view.GONE);

    }

    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        super.onPageStarted(view, url, favicon);
    }

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
       view.loadUrl(url);
       return true;

    }


}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if ((keyCode == KeyEvent.KEYCODE_BACK) && web.canGoBack())
    {web.goBack();
    return true;}

    return super.onKeyDown(keyCode, event);
}

}

最佳答案

1.)web.setWebViewClient(new WebViewClient() {将覆盖上一个集合webviewnew myWebClient(),因此删除此

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

2.)将onReceivedError移到myWebClient类中,这样代码将
public class YazdMap extends ActionBarActivity {

    WebView web;
    ProgressBar progressBar;
    TextView txt;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_yazd_map);

        web = (WebView) findViewById(R.id.webview1);
        progressBar = (ProgressBar) findViewById(R.id.progressBar);
        txt = (TextView) findViewById(R.id.txt);

        web.setWebViewClient(new MyWebClient());
        web.getSettings().setJavaScriptEnabled(true);
        web.loadUrl("http://google.com/");

    }

    public class MyWebClient extends WebViewClient
    {

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                progressBar.setVisibility(view.GONE);
                txt.setVisibility(view.GONE);

            }

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
               view.loadUrl(url);
               return true;

            }

            @Override
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
               view.loadUrl("file:///android_asset/error.html");
            }

        }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && web.canGoBack())
        {web.goBack();
        return true;}

        return super.onKeyDown(keyCode, event);
    }
}

注意:我将名称从myWebClient改为MyWebClient,因为根据惯例,类必须有第一个字母作为大写。

10-08 17:15