构建时出现Android Toast.makeText错误

时间:2019-04-05 18:30:13

标签: android

error: no suitable method found for makeText(<anonymous OnCompleteListener<AuthResult>>,String,int)
method Toast.makeText(Context,CharSequence,int) is not applicable
(argument mismatch; <anonymous OnCompleteListener<AuthResult>> cannot be converted to Context)
method Toast.makeText(Context,int,int) is not applicable
(argument mismatch; <anonymous OnCompleteListener<AuthResult>> cannot be converted to Context)

1 个答案:

答案 0 :(得分:0)

makeText()方法定义为:

public static Toast makeText(Context context, CharSequence text, int duration)

显然,您正在使用侦听器作为上下文。

改为使用:

Toast.makeText([ACTIVIY NAME].this, "Message...", Toast.LENGTH_SHORT).show();

如果您位于Activity内,则可以使用getApplicationContext()来获取上下文

Toast.makeText(getApplicationContext(), "Message...", Toast.LENGTH_SHORT).show();

如果您位于Fragment内,则可以使用getActivity()来获取上下文

Toast.makeText(getActivity(), "Message...", Toast.LENGTH_SHORT).show();