简体   繁体   English

吐司不适用于参数

[英]Toast is not applicable for the arguments

I am self studying android projects and i am very new to android and java. 我是在自学android项目,对android和java还是很陌生。 i cant figure out the error, please help, 我无法找出错误,请帮忙,

Toast.makeText(this, msg, Toast.LENGTH_LONG).show();

this gives me the error that i pasted below: The method makeText(Context, CharSequence, int) in the type Toast is not applicable for the arguments (new BleWrapperUiCallbacks.Null(){}, String, int) 这给了我我下面粘贴的错误: Toast类型的方法makeText(Context, CharSequence, int)不适用于参数(new BleWrapperUiCallbacks.Null(){}, String, int)

when i use "context" instead of "this", there is no error. 当我使用“上下文”而不是“ this”时,没有错误。 if i want to use "this" keyword means what should i do? 如果我想使用“ this”关键字,那该怎么办? and what is the use of "this" keyword on toast.maketext function? toast.maketext函数中“ this”关键字的用途是什么? thanking you. 感谢您。

如果要使用this作为匿名内部类(例如OnClickListener )中Activity的指针,则必须在this之前使用Activity名称: MyActivity.this ,用Activity的名称替换。

this - 这个 -

Within an instance method or a constructor, this is a reference to the current object 在实例方法或构造函数中,这是对当前对象的引用

You may use this only on object that are extending Context like Activity - 您只能在扩展Context之类的对象(例如Activity)上使用此对象-

Activity extends ContextThemeWrapper The method 活动扩展了ContextThemeWrapper方法

Toast.makeText(MainActivity.this, CharSequence, int)

in other case you should use context reference 在其他情况下,您应该使用上下文引用

You could only use this keyword if you have this code in the context of the Activity class. 如果在Activity类的上下文中具有此代码,则只能使用this关键字。
All gui in android have its context -> for example Activity, so to create object like toast you need to pass an Context . android中的所有gui都有其context->例如Activity,因此要创建类似toast的对象,您需要传递Context
If your method, in other class than Activity Subclass is using this , pass a new parameter to your method -> Context context , and use it Like (in Activity class): 如果您的方法在Activity Subclass之外的其他类中正在使用this ,请将新参数传递给您的方法-> Context context ,并使用Like(在Activity类中):
MyClass.myFunction(YourActivity.this);

If u want to use this keyword, u can use like this, 如果您想使用此关键字,可以这样使用,

Toast.makeText(Classname.this, msg , Toast.LENGTH_LONG).show();

Where Classname is your android class name 其中Classname是您的android类名称

Toast.makeText(youractivityname.this, "your message text", Toast.LENGTH_LONG).show();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM