gpt4 book ai didi

java - Activity context.this 无法解析为变量(在 Toast 内)

转载 作者:行者123 更新时间:2023-11-29 05:33:07 24 4
gpt4 key购买 nike

我正在尝试在我的 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),然后它工作正常..我做错了什么?

最佳答案

如果你的类通过 Activity 扩展意味着像这样使用

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

关于java - Activity context.this 无法解析为变量(在 Toast 内),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20423701/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com