android Toast报错 Activity context.this cannot be resolved to a variable

android开发,我在我的 activity 插入一个按钮,如果andriod market找不到的话就使用 toast进行提示,在使用的时候出现了一些问题,下面的代码报错了 Context cannot be resolved to a variable 

1
2
3
4
5
6
7
Uri uri = Uri.parse( "market://details?id=" + getApplicationContext().getPackageName());
Intent goToMarket = new Intent(Intent.ACTION_VIEW, uri);
try {
     startActivity(goToMarket);
} catch (ActivityNotFoundException e) {
     Toast.makeText(Activity. this , "Couldn't launch the market" , Toast.LENGTH_LONG).show();
}

我试过如下调用

Toast.makeText(this, "Couldn't launch the market", Toast.LENGTH_LONG).show();
然后开发工具提示 The method makeText(Context, CharSequence, int) in the type Toast is not applicable for the arguments (new View.OnClickListener(){}, String, int)

我以前这么使用没有问题,不明白现在怎么就不行了

处理方法

你过你继承了Activity,就使用下面的2个方式
Toast.makeText(ClassName.this, "Couldn't launch the market",Toast.LENGTH_LONG).show();
或者
Toast.makeText(getApplicationContext(), "Couldn't launch the market",Toast.LENGTH_LONG).show();

如果你的类继承了 Fragment ,就使用下面的方式
Toast.makeText(getActivity(), "Couldn't launch market",Toast.LENGTH_LONG).show();


原文地址:http://www.itmmd.com/201411/153.html 
该文章由 萌萌的IT人 整理发布,转载须标明出处。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值