I am getting error while creating a Toast
Toast toast = Toast.makeText(this, text, duration);
I am getting cannot resolve makeText()
method of Toast
.
I am getting this error
java: no suitable method found for makeText(idtech.ESDN.ShapeData,java.lang.CharSequence,int)
method android.widget.Toast.makeText(android.content.Context,int,int) is not applicable
(actual argument idtech.ESDN.ShapeData cannot be converted to android.content.Context by method invocation conversion)
method android.widget.Toast.makeText(android.content.Context,java.lang.CharSequence,int) is not applicable
(actual argument idtech.ESDN.ShapeData cannot be converted to android.content.Context by method invocation conversion)
The makeText's signature is the following
public static Toast makeText (Context context, CharSequence text, int duration)
the first paramter has to be a context object. You put this
, but this
refers to this object and it can be something different from an Activity
(a Fragment
for instance).
this in your case might not be the object of the activity. You might be using the Toast.makeText method inside you Click Listener object. To resolve this you need to use getApplicationContext() :
Toast.makeText(getApplicationContext() , "Your Message", Toast.LENGTH_LONG);
Have you imported the toast widget?
import android.widget.Toast;
You can also call show() in the same line if you want to output it straight away:
Toast toast = Toast.makeText(context, text, duration).show();
Hope that helps.
In the onClick(View view)
click listener within a RecyclerView.ViewHolder
the context is retrieved with view.getContext()
, as in:
```
public class MyHolder extends RecyclerView.ViewHolder implements
View.OnClickListener {
public MyHolder(View itemView) {
super(itemView);
//...
itemView.setOnClickListener(this);
}
@Override
public void onClick(View view) {
Toast.makeText(view.getContext(), "the message",
Toast.LENGTH_SHORT).show();
}
```
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With