Java 活动上下文。无法将其解析为变量(在Toast内部)
我正在尝试在我的Java 活动上下文。无法将其解析为变量(在Toast内部),java,android,android-intent,onclicklistener,android-toast,Java,Android,Android Intent,Onclicklistener,Android Toast,我正在尝试在我的活动中插入一个按钮对应用程序进行评分,如果找不到市场,则插入一个土司。但我在活动中得到一个“上下文无法解析为变量”。这: Uri uri = Uri.parse("market://details?id=" + getApplicationContext().getPackageName()); Intent goToMarket = new Intent(Intent.ACTION_VIEW, uri); try { startActivity(goToMarket);
活动中插入一个按钮
对应用程序进行评分,如果找不到市场,则插入一个土司
。但我在活动中得到一个“上下文无法解析为变量”
。这:
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();
但我在这条线上有多个标记
-Toast类型中的方法makeText(Context,CharSequence,int)
不适用于参数(new View.OnClickListener(){},String,int)
我以前做过一个简单的按钮吐司
(没有尝试
/捕获
),然后效果很好。。
我做错了什么?试试:
Toast.makeText(getApplicationContext(), "Couldn't launch the market", Toast.LEGTH_LONG).show();
你的回答是:
Toast.makeText(getApplicationContext(), "Couldn't launch the market", Toast.LENGTH_LONG).show();
如果您的类是通过活动扩展的,请使用以下方法
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();
试试这个
Uri uri = Uri.parse("market://details?id="
+ getApplicationContext().getPackageName());
Intent goToMarket = new Intent(Intent.ACTION_VIEW, uri);
try {
startActivity(goToMarket);
} catch (ActivityNotFoundException e) {
this.runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),
"Couldn't launch the market", Toast.LENGTH_LONG)
.show();
}
});
}
希望这将帮助您……使用这一个Toast.makeText(getApplicationContext(),“无法启动市场”,Toast.LENGTH\u LONG.show();谢谢你的快速回复-它成功了。我以前尝试过getApplicationContext(),但它不起作用,但现在起作用了-上次我一定做错了什么。。再次感谢!谢谢你的快速回复-它成功了。我以前尝试过getApplicationContext(),但它不起作用,但现在起作用了-上次我一定做错了什么。。再次感谢!