Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

error to startActivity(intent), whats going wrong?

Below here in code startActivity(intent) gives me an error

Here's my code:

public class MyWebViewClient3 extends WebViewClient {

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if (Uri.parse(url).getHost().equals("www.facebook.com")) {
            // This is my web site, so do not override; let my WebView load the page
            return false;
        }
        // Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        startActivity(intent);//this is where it goes wrong
        return true;
        }
}
like image 277
Gaurav Avatar asked Dec 27 '22 07:12

Gaurav


1 Answers

WebViewClient client is not a context , so you cannot start Activity from here.. You might want to get Context as a reference and then say

context.startActivity(intent);

After vmironov's suggestion..

@Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if (Uri.parse(url).getHost().equals("www.facebook.com")) {
            // This is my web site, so do not override; let my WebView load the page
            return false;
        }
        // Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        view.getContext().startActivity(intent);
        return true;
    }
like image 50
ngesh Avatar answered Dec 28 '22 23:12

ngesh