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