为什么我无法通过Android WebView调用onPageStarted,onReceivedError或onPageFinished?

时间:2016-12-09 20:31:02

标签: android webview

我希望能够识别错误发生的时间并告诉我显示带有错误的Toast消息,但我永远无法让它们调用。 我还希望能够告诉webview什么时候开始导航到一个网址并检测它会去哪个网址。

不幸的是,它似乎忽略了所有内容,我不知道如何启动一个函数或方法或其他任何事情。我真的不在乎如何确定页面何时开始加载或发生错误我只需要一种有效的方式。

public class MainActivity extends AppCompatActivity {

private WebView myWebView;

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

        myWebView = (WebView) findViewById(R.id.webview);

       myWebView.setWebViewClient(new WebViewClient());
       myWebView.setWebChromeClient(new WebChromeClient()
        {

            private boolean error;


            public void onPageStarted(WebView view, String url, Bitmap favicon) {

                onPageStarted(view, url, favicon);
                Toast.makeText(getApplicationContext(), "page started:" + url, Toast.LENGTH_SHORT).show();
                error = false;
            }


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

                error = true;
                System.out.println("description error" + description);
                view.setVisibility( View.GONE );
                Toast.makeText(getApplicationContext(), "error:" + description, Toast.LENGTH_SHORT).show();
            }


            public void onPageFinished(WebView view, String url) {

                if (!error) {
                    view.setVisibility( View.VISIBLE );
                }
                error = false;

                Toast.makeText(getApplicationContext(), "page finished" + url, Toast.LENGTH_SHORT).show();
            }
        }
        );





        myWebView.setBackgroundColor(0);
        WebSettings webSettings = myWebView.getSettings();
        webSettings.setDefaultTextEncodingName("utf-8");
        webSettings.setJavaScriptEnabled(true);
        webSettings.setLoadWithOverviewMode(true);
        webSettings.setAllowFileAccess(true);


        // Other webview settings
        myWebView.addJavascriptInterface(new MyJavascriptInterface(this), "Android");

        myWebView.loadUrl("https://example.com/");



    }

}

1 个答案:

答案 0 :(得分:0)

onPageStarted()中未定义onPageFinished()onReceivedError()@Override方法。他们是WebChromeClient class的成员。你把错误的子类化了。

如果在每个方法上面添加WebViewClient注释,IDE会对你大吼大叫这些方法不会覆盖超类中的任何内容,这就是注释有用的原因。

只需将这些覆盖移到myWebView.setWebViewClient(new WebViewClient() { private boolean error; @Override public void onPageStarted(WebView view, String url, Bitmap favicon) { ... } @Override public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { ... } @Override public void onPageFinished(WebView view, String url) { ... } } ); myWebView.setWebChromeClient(new WebChromeClient()); 实例。

win = nw.Window.open(
  'view.html',
  {
    width: 300,
    height: 400,
    show: false, // I show the window when DOM has been loaded.
  },
  function( win ) {
    win.setShowInTaskbar( false );

    win.on( 'close', function() {
      this.hide();
    } );
  }
);