错误给startActivity(意向),什么回事? [英] error to startActivity(intent), whats going wrong?

查看:153
本文介绍了错误给startActivity(意向),什么回事?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面在这里code startActivity(意向)给我一个错误

下面是我的code:

 公共类MyWebViewClient3扩展WebViewClient {    @覆盖
    公共布尔shouldOverrideUrlLoading(的WebView视图,字符串URL){
        如果(Uri.parse(URL).getHost()。等于(www.facebook.com)){
            //这是我的网站,所以不要覆盖;让我的WebView加载页面
            返回false;
        }
        //否则,该链接不上我的网站页面,因此启动该处理URL另一活动
        意向意图=新意图(Intent.ACTION_VIEW,Uri.parse(URL));
        startActivity(意向); //这是哪里出了问题
        返回true;
        }
}


解决方案

WebViewClient客户端不是一个背景,所以你不能从这里开始的活动。你可能想要获取上下文作为参考,然后说

  context.startActivity(意向);

vmironov 的建议。

  @覆盖
    公共布尔shouldOverrideUrlLoading(的WebView视图,字符串URL){
        如果(Uri.parse(URL).getHost()。等于(www.facebook.com)){
            //这是我的网站,所以不要覆盖;让我的WebView加载页面
            返回false;
        }
        //否则,该链接不上我的网站页面,因此启动该处理URL另一活动
        意向意图=新意图(Intent.ACTION_VIEW,Uri.parse(URL));
        。view.getContext()startActivity(意向);
        返回true;
    }

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

解决方案

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

这篇关于错误给startActivity(意向),什么回事?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆