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

时间: 2024-08-09 02:18:15

android Toast报错 Activity context.this cannot be resolved to a variable的相关文章

android模拟器 报错:X Error of failed request: BadRequest (invalid request code or no such operation)

最近在ubuntu12.04上学习python,python2.7 python3.2差别还是蛮大的,就想着学习较新的 升级后 结果出现 输入法不显示   update-manager 和  add-apt-repository不能用,android的模拟器也打不开. 罪魁祸首直至python 因为python3.2没有往上兼容,导致从python2.7升级到3.2 很多依赖之前2.7版本的功能无法使用 如何解决python升级的问题 请 http://book.51cto.com/art/20

因为ndk路径错误的android打包报错

执行:编译cocos2d里的android项目 报错:[taskdef] could not load definitions from resource anttasks. properties. It could not be found 原因:/F:/bundle/android-ndk/android-ndk-r10d  ndk路径前面多了一个斜杠  "/" 知识点:window的路径前面没有"/"    linux和mac的才是"/"

Android 工程报错解决 Unable to resolve target 'android-17'

转自:http://www.cnblogs.com/csulennon/p/3705177.html 换了系统后,重新安装了Android SDK和ADT插件,导入之前的工作空间.居然发现所有的Android工程都报错了. 我之前的SDK版本是用的17,升级后用的是19. 1: [2014-05-03 14:56:25 - ThinkBlink] Unable to resolve target 'android-17' 2: [2014-05-03 14:56:26 - Game2048Pub

关于子线程使用Toast报错Can't create handler inside thread that has not called Looper.prepare()的解决办法

形同如下代码,在Thread中调用Toast显示错误信息: new Thread(new Runnable(){ @Override public void run() { try{ weatherData = getWeatherData(strUrl); parseJson(weatherData); }catch(Exception e){ Toast.makeText(WindowApplication.getAppContext(), e.toString(), Toast.LENGT

Android打包报错 Export aborted because fatal lint errors were found. These are listed in the Lint View

打包时报如下错误: Export aborted because fatal lint errors were found. These are listed in the Lint View. Either fix these before running Export again,or turn off"Run full error check when exporting app" in the Android > Lint Error Checking preferenc

Android Studio报错:Failed to complete Gradle execution.

解决Android Studio报错:Failed to complete Gradle execution. 运行Android Studio的时候,出现以下错误: 原因:用于启动虚拟机的内存不够,android studio的默认最大内存是10g,所以对于4g.8g内存的电脑来说,内存不够用,不过为什么有时不会出问题有时会,可能是google优化不好吧,但这又有个问题,把参数改为1g.2g都不行,一定要更小才行,只能说明Android studio确实有bug. 解决方法 1: 解决方法 2

Android handler 报错处理Can't create handler inside thread that has not called Looper.prepare()

解决方法,在ui线程里面创建handler m_MainActivity.runOnUiThread(new Runnable(){ @Override public void run() { // TODO Auto-generated method stub GameBoxUtil.startPay(m_MainActivity,String.valueOf(amount), productName, payCode,orderId,payHandler); }}); Android han

【已解决】unity4.2.0f4 导出Android工程报错:Error building Player: ArgumentException: Illegal characters in path.

你看到的这个文章来自于http://www.cnblogs.com/ayanmw 使用unity3D开发的一个客户端,需要导出为Android工程,然后接入一些第三方android SDK. unity版本 操作系统为: OS 名称: Microsoft Windows 7 旗舰版 OS 版本: 6.1.7601 Service Pack 1 Build 7601 OS 制造商: Microsoft Corporation OS 配置: 独立工作站 OS 构件类型: Multiprocessor

Android studio报错 Error retrieving parent for item: No resource found that matches the given name 'android:TextAppearance.Material.Inverse'

用Android studio创建一个工程,我选择了Phone and Tablet平台, Minimum SDK选择了API 9,也就最低支持是Android 2.3, 接下来一路Next.惊喜出现了,报了一堆 Error retrieving parent for item: No resource found that matches the given name 'android:TextAppearance.Material.Inverse'... 都是说找不到某个样式,仔细看错误信息