-2

当我尝试使用shouldOverrideUrlLoading()in实现 Chrome 自定义选项卡webViewClient()时,出现以下错误:

第一个参数类型错误。找到:'android.webkit.WebViewClient',需要:'android.app.Activity'

这是我的代码 - 我正在使用这个 GitHub 存储库https://github.com/GoogleChrome/custom-tabs-client。该错误来自我对this关键字的使用。

我在片段中,而不是在活动中

mWebView.setWebViewClient(new WebViewClient() {

    public boolean shouldOverrideUrlLoading(WebView view, String url) {

        CustomTabsIntent customTabsIntent = new CustomTabsIntent.Builder().build();
        CustomTabActivityHelper.openCustomTab(this, customTabsIntent, Uri.parse(url), new WebviewFallback());

        Toast toast = Toast.makeText(getApplicationContext(),
        "This is a message displayed in a Toast" + url, Toast.LENGTH_SHORT);

        toast.show();
        return true;
    }

}
4

3 回答 3

5

您正在使用thisis 在匿名类 ( new WebViewClient()) 的上下文中,因此this指的是WebViewClient.

由于您使用的是片段,因此可以替换thisgetActivity()

CustomTabActivityHelper.openCustomTab(getActivity(), customTabsIntent, Uri.parse(url), new WebviewFallback());
于 2015-09-08T12:07:08.330 回答
1

看起来“这个”是你的 WebViewClient 而不是你的活动。如果您显示的代码在 Activity 中,请尝试使用“YourActivity.this”来指定。

希望它有所帮助。

于 2015-09-08T12:13:04.823 回答
0

这可能取决于 android 构建版本问题,以下代码将在 2.3+ 构建上成功运行,检查一下

ourBrow.setWebViewClient(new WebViewClient() {
    @Override
    public void onReceivedError(WebView view, int errorCode,
        String description, String failingUrl) {
            Log.d("WEB_VIEW_TEST", "error code:" + errorCode + " - " + description);
    }

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
            // handle different requests for different type of files
            // this example handles downloads requests for .apk and .mp3 files
            // everything else the webview can handle normally
            if (url.endsWith(".apk")) {
                Uri source = Uri.parse(url);
                // Make a new request pointing to the .apk url
                DownloadManager.Request request = new DownloadManager.Request(source);
                // appears the same in Notification bar while downloading
                request.setDescription("Description for the DownloadManager Bar");
                request.setTitle("YourApp.apk");
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
                    request.allowScanningByMediaScanner();
                    request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
                }
                // save the file in the "Downloads" folder of SDCARD
                request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "SmartPigs.apk");
                // get download service and enqueue file
                DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
                manager.enqueue(request);
            }
            else if(url.endsWith(".mp3")) {
                // if the link points to an .mp3 resource do something else
            }
            // if there is a link to anything else than .apk or .mp3 load the URL in the webview
            else view.loadUrl(url);
            return true;                
    }
});
于 2015-09-08T12:37:00.127 回答