码迷,mamicode.com
首页 > 移动开发 > 详细

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

时间:2014-12-17 18:35:36      阅读:192      评论:0      收藏:0      [点我收藏+]

标签:android

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人 整理发布,转载须标明出处。

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

标签:android

原文地址:http://blog.csdn.net/androidmylove/article/details/41984073

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!