[英]Why can't I get onPageStarted, onReceivedError, or onPageFinished to ever call with Android WebView?
I want to be able to recognize when an error occurs and tell show a toast message with the error but I can never get them to call. 我希望能够识别何时发生错误,并告诉显示带有该错误的祝酒消息,但我永远无法让他们打电话。 I also want to be able to tell when the webview starts navigating to a url and detect which url it's going to. 我还希望能够告诉webview何时开始导航到url并检测它要去往哪个url。
Unfortunately it just seems to ignore everything and I have no idea how to start a function or method or anything once this occurs.I don't really care how I go about determining when a page starts loading or an error occurs I just need a way that works. 不幸的是,它似乎忽略了所有内容,并且我不知道一旦发生这种情况如何启动函数或方法,我真的不在乎如何确定何时开始加载页面或发生错误,我只需要一种方法这样可行。
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/");
}
}
The onPageStarted()
, onPageFinished()
, and onReceivedError()
methods are not defined in the WebChromeClient
class . 所述onPageStarted()
onPageFinished()
和onReceivedError()
方法未在规定WebChromeClient
类 。 They're members of WebViewClient
. 他们是WebViewClient
成员。 You subclassed the wrong one. 您把错误的子类化了。
If you add the @Override
annotation above each method, your IDE should yell at you that those methods don't override anything in the super class, which is why that annotation is useful. 如果在每个方法上方添加@Override
批注,则IDE会大吼大叫这些方法不会覆盖超类中的任何内容,这就是为什么该批注很有用的原因。
Simply move those overrides to the WebViewClient
instance. 只需将这些替代移动到WebViewClient
实例。
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());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.