为什么我的shouldOverrideLoading方法永远不会被调用?

时间:2016-06-11 02:59:13

标签: android webview

我试图在shouldOverrideLoading上展示祝酒词,然而,它永远不会被调用。我的代码很大,所以我把它拆成了一个更小的代码,但我仍然无法找到我正在犯的错误。这是代码:

public class MainActivity extends Activity {

private WebView webView;
private EditText urlEditText;
private ProgressBar progress;

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

    urlEditText = (EditText) findViewById(R.id.urlField);
    webView = (WebView) findViewById(R.id.webView);
    webView.setWebChromeClient(new MyWebViewClient());
    CookieManager.getInstance().setAcceptCookie(true);// Enable Cookies
    webView.getSettings().setJavaScriptEnabled(true);// Enable Java Script
    webView.setWebViewClient(new ViewClient());

    webView.getSettings().setPluginState(PluginState.ON);// Enable Flash

    webView.getSettings().setLoadsImagesAutomatically(true);
    webView.setWebViewClient(new WebViewClient() {// ************************************
        // ON PAGE LOAD AND FINISHED
        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            InputMethodManager imm = (InputMethodManager) getSystemService(// hide_keyboard
            Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(urlEditText.getWindowToken(), 0);
            super.onPageStarted(view, url, favicon);
        }

        @Override
        public void onPageFinished(WebView view, String url) {
        }

        // **//
        @Override
        public void onReceivedError(WebView view, int errorCode,
                String description, String failingUrl) {
            Toast.makeText(getBaseContext(), "Pull down to refresh",
                    Toast.LENGTH_LONG).show();
            view.loadUrl("about:blank");
            super.onReceivedError(view, errorCode, description, failingUrl);
        }
    });
    progress = (ProgressBar) findViewById(R.id.progressBar);
    progress.setMax(100);
    // Use Done/search button in keyboard as go button
    urlEditText = (EditText) findViewById(R.id.urlField);
    urlEditText.setOnEditorActionListener(new OnEditorActionListener() {
        public boolean onEditorAction(TextView arg0, int arg1, KeyEvent arg2) {
            CustomSearch();
            return false;
        }
    });
}

private class MyWebViewClient extends WebChromeClient {
    @Override
    public void onProgressChanged(WebView view, int newProgress) {
        MainActivity.this.setValue(newProgress);
        super.onProgressChanged(view, newProgress);
    }
}

public void setValue(int progress) {
    this.progress.setProgress(progress);
}

class ViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView webview, String url) {
        Toast.makeText(getBaseContext(), "page started", Toast.LENGTH_LONG)
                .show();
        webview.loadUrl(url);
        return true;
    }
}

public void CustomSearch() {
    // go button for searching worldwide
    urlEditText = (EditText) findViewById(R.id.urlField);
    String url = urlEditText.getText().toString();
    webView.loadUrl(url);
}

}

1 个答案:

答案 0 :(得分:1)

问题在于:

1

首先,您将webView.setWebViewClient(new ViewClient()); webView.getSettings().setPluginState(PluginState.ON);// Enable Flash webView.getSettings().setLoadsImagesAutomatically(true); webView.setWebViewClient(new WebViewClient() { // ... } 实例传递给ViewClient,然后在不使用它的情况下,使用一个简单的setWebViewClient()实例覆盖它,该实例没有您的覆盖{{ 1}}方法。