I am self studying android projects and i am very new to android and 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)
when i use "context" instead of "this", there is no error. if i want to use "this" keyword means what should i do? and what is the use of "this" keyword on toast.maketext
function? 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 -
Activity extends ContextThemeWrapper The method
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.
All gui in android have its context -> for example Activity, so to create object like toast you need to pass an 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):
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
Toast.makeText(youractivityname.this, "your message text", Toast.LENGTH_LONG).show();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.