startActivity(意图)错误,出错了什么?

Gau*_*rav 4 url tabs android webview android-intent

下面的代码startActivity(intent)给出了一个错误

这是我的代码:

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;
        }
}
Run Code Online (Sandbox Code Playgroud)

nge*_*esh 10

WebViewClient客户端不是上下文,因此您无法从此处启动Activity ..您可能希望将Context作为参考然后再说

context.startActivity(intent);
Run Code Online (Sandbox Code Playgroud)

vmironov的建议之后......

@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;
    }
Run Code Online (Sandbox Code Playgroud)