標籤:android開發 android
android開發,我在我的 activity 插入一個按鈕,如果andriod market找不到的話就使用 toast進行提示,在使用的時候出現了一些問題,下面的代碼報錯了 Context cannot be resolved to a variable
| 1234567 |
Uri uri = Uri.parse("market://details?id="+ getApplicationContext().getPackageName());Intent goToMarket = newIntent(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