【问题标题】:Error 'Cannot resolve method makeText' when creating a Toast [duplicate]创建 Toast 时出现错误“无法解析方法 makeText”[重复]
【发布时间】:2020-03-05 22:06:30
【问题描述】:

当我在这里使用此代码时

Toast.makeText(HomeActivity.this, ""+accountKitError.getErrorType().getMessage());

我收到一条消息无法解析方法'makeText。 这是我的代码:

@Override
public void onError(AccountKitError accountKitError) {
    Toast.makeText(HomeActivity.this, "+accountKitError.getErrorType().getMessage());
}

【问题讨论】:

标签: java android android-studio toast android-toast


【解决方案1】:

makeText 方法采用三个参数:应用程序contexttext 消息和用于 toast 的duration。它返回一个正确初始化的 Toast 对象。您可以使用show() 显示 toast 通知,如下例所示:

Context context = getApplicationContext();
CharSequence text = "Hello toast!";
int duration = Toast.LENGTH_SHORT;

Toast toast = Toast.makeText(context, text, duration);
toast.show();

在您的情况下,您缺少 durationshow(),像这样添加它们,它会起作用:

Toast.makeText(
    HomeActivity.this, 
    ""+accountKitError.getErrorType().getMessage(),
    Toast.LENGTH_SHORT
).show();

这里是有关 Toast 的更多信息的文档链接: https://developer.android.com/guide/topics/ui/notifiers/toasts#java

【讨论】:

  • 非常感谢!!现在它运行良好。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-15
  • 1970-01-01
相关资源
最近更新 更多