我有以下简单的活动:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
wv = (WebView) findViewById(R.id.webview);
wv.setWebChromeClient(new CustomWebViewClient());
}
我找到了以下自定义网络客户端的代码段,并希望在上述活动中使用它:
private class CustomWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(url.contains("mysite.com")) {
view.loadUrl(url);
} else {
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(i);
}
return true;
}
}
我收到错误:
错误:(53,11)错误:类WebView中的方法setWebChromeClient 不能适用于给定的类型;必需:WebChromeClient发现: MainActivity.CustomWebViewClient原因:实际参数 MainActivity.CustomWebViewClient无法转换为 WebChromeClient通过方法调用转换
我做错了什么?
答案 0 :(得分:0)
如果您的意思是使用WebViewClient
,那么只需将WebViewClient
替换为WebChromeClient
,并使用带WebViewClient
个对象的方法,即
wv.setWebViewClient(new CustomWebViewClient());
您需要在WebChromeClient
中展开CustomWebViewClient
。
setWebChromeClient()
需要WebChromeClient
个对象作为参数。
private class CustomWebViewClient extends WebViewClient {
到
private class CustomWebViewClient extends WebChromeClient {
答案 1 :(得分:0)
我相信你混淆了WebViewClient和WebChromeClient。如果您正在调用setWebChromeClient
方法,则参数应来自WebChromeClient
,而不是WebViewClient
,WebViewClient
应使用setWebViewClient
。