活动context.this无法解析为变量(在Toast内)

时间:2013-12-06 12:15:55

标签: java android android-intent onclicklistener android-toast

我正在尝试在我的button中为{app}评分应用activity,如果找不到市场则会toast。但是我在Activity.this上得到了一个“Context cannot be resolved to a variable”:

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)

之前我做过一个简单的button toast方式(没有try / catch),然后就可以了。 我做错了什么?

4 个答案:

答案 0 :(得分:3)

如果您的课程正在扩展活动,则意味着像这样使用

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();

答案 1 :(得分:0)

尝试:

Toast.makeText(getApplicationContext(), "Couldn't launch the market", Toast.LEGTH_LONG).show();

答案 2 :(得分:0)

你的回答:

Toast.makeText(getApplicationContext(), "Couldn't launch the market", Toast.LENGTH_LONG).show();

答案 3 :(得分:0)

试试这个......

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();
                }
            });
        }

希望这会对你有帮助......