-2

When I tried to implement Chrome Custom Tabs using shouldOverrideUrlLoading() in webViewClient() I am getting the following error:

Wrong 1st argument type. Found: 'android.webkit.WebViewClient', required: 'android.app.Activity'

Here is my code - I am using this GitHub repository https://github.com/GoogleChrome/custom-tabs-client. The error is coming from my usage of the this keyword.

I am in Fragment, not in Activity

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;
    }

}
Prashanth
  • 1,309
  • 2
  • 9
  • 17

3 Answers3

5

You are using this is within the context of an anonymous class (new WebViewClient()), so this is referring a type of WebViewClient.

Since you're using a fragment, you can replace this with getActivity():

CustomTabActivityHelper.openCustomTab(getActivity(), customTabsIntent, Uri.parse(url), new WebviewFallback());
anon
  • 4,163
  • 3
  • 29
  • 26
  • Thanks for the answer, if i am inside a fragment, what should i use ? – Prashanth Sep 08 '15 at 12:36
  • `CustomTabActivityHelper.openCustomTab(getActivity().this, customTabsIntent, Uri.parse(url), new WebviewFallback());` It is showing error at this as ") expected" am i missing anything here? – Prashanth Sep 08 '15 at 13:11
  • 1
    You don't need `.this` after `getActivity()`. – anon Sep 08 '15 at 13:15
1

Looks like "this" is your WebViewClient not your Activity. If your shown code is in a Activity, try to use "YourActivity.this" to specify.

Hope it helped.

user3485077
  • 146
  • 2
  • I am using inside a fragment, so what should i use ? – Prashanth Sep 08 '15 at 12:34
  • 1
    You can use [getActivity()](http://developer.android.com/reference/android/app/Fragment.html#getActivity%28%29) to get the activity of the fragment. – user3485077 Sep 08 '15 at 13:01
  • `CustomTabActivityHelper.openCustomTab(getActivity().this, customTabsIntent, Uri.parse(url), new WebviewFallback());` It is showing error at this as ") expected" am i missing anything here? – Prashanth Sep 08 '15 at 13:06
  • Because you are in an anonymous class you need to call `YourFragment.this.getActivity()`. – user3485077 Sep 08 '15 at 13:11
0

It may be depends on the android build version problem, the below code will work successfully on 2.3+ builds, check this out

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;                
    }
});
Nooruddin Lakhani
  • 7,507
  • 2
  • 19
  • 39