Reputation: 1094
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)
Upvotes: 31
Views: 80599
Reputation: 13
Using hard coded text worked fine but I had this error when I use a string from the values' resource folder, if this is your case try this:
1- Right click on the hardcoded text and extract a string resource anyway
2- undo the string resource extraction using CTRL+Z
3- redo the string resource extraction using CTRL+y
This worked for this case and the error disapeared.
Upvotes: 0
Reputation: 3
assuming you are creating toast in fragment ....so use getactivity in context
assuming you are creating toast in fragment ....so use getactivity in context
Toast.makeText(getActivity,"Your Text",Toast.LENGTH_SHORT).show();
Upvotes: 0
Reputation: 17
Just like Blackbelt said the syntax of Toast is as follows:
Toast.makeText(Activity.this, "Message",Toast.<specify Lenght>).show();
where Activity.this
is the current activity, Message
is the string you want to show and Toast.length
is the length you want it to show it for.
Upvotes: 0
Reputation: 1
in the onClick method try this
Toast.makeText(view.getContext(), "sorry", Toast.LENGTH_LONG).show();
it did work form me.
Upvotes: 0
Reputation: 174
In case of a Toast in a fragment inside a Tabbed Activity, use getContext() e.g.
Toast.makeText(getContext(), "Your Text Here", Toast.LENGTH_SHORT).show();
Upvotes: 2
Reputation: 159
Instead of
Toast toast = Toast.makeText(this, text, duration);
Add your activity name before "this" word
Toast toast = Toast.makeText(MyActivity.this, text, duration);
Upvotes: 2
Reputation: 1931
First add
import android.widget.Toast;
statement if you did not already and then
Toast.makeText(YourActvityName.this, "Your Text", Toast.LENGTH_SHORT).show();
Upvotes: 0
Reputation: 436
Was having similar issue too but
getContext()
did the trick for me
// If message field is empty show a toast and alert the user
if (TextUtils.isEmpty(message)) {
Toast.makeText(getContext(),"Please Enter a message", Toast.LENGTH_SHORT).show();
return;
}
Upvotes: 0
Reputation: 2106
Grab the context from the calling activity (eg. this or MainActivity.this) and pass it into the method your Toast resides in. That way it lives together with the calling activity.
Application context, which you get from getApplicationContext() and getContext() is mainly for long running processes. Using it for short lived processes can lead to memory leaks.
Upvotes: 1
Reputation: 321
I have faced similar problem in android studio, I resolve this issue by using getActivity()
instead of this
in the fragment
Toast.makeText(getActivity(), "Your Text", Toast.LENGTH_SHORT).show();
Upvotes: 1
Reputation: 164
This might be helpful if you are trying to use Toast in Fragment:
Toast.makeText(Your_Fragment_Name.super.getContext(), "Added", Toast.LENGTH_SHORT).show();
Upvotes: 0
Reputation: 875
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();
}
```
Upvotes: 7
Reputation: 2678
If you are trying to Toast
your text in the MainActivity then do this:
Toast.makeText(getApplicationContext(), "Your text", Toast.LENGTH_LONG).show();
Upvotes: 1
Reputation: 173
I have faced a similar problem but in my case i found out that Xamarin c# and Java in Android studio have differences when calling some functions(same functions).
When using Xamarin and c#, then makeText becomes MakeText and show becomes Show as shown below:
Toast toast = Toast.MakeText(this, "Text", ToastLength.Long);
toast.Show();
Hope this helps:)
Upvotes: 0
Reputation: 1204
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);
Upvotes: 28
Reputation: 2209
Make sure that you type:
Toast toast = Toast.makeText(this, text, duration);
Not:
Toast toast = new Toast.makeText(this, text, duration);
Upvotes: 5
Reputation: 557
Try Toast toast = Toast.makeText(getActivity(), text, duration);
You may also wish to append .show()
if you want it to display
Upvotes: 3
Reputation: 157457
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).
Upvotes: 35
Reputation: 4799
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.
Upvotes: 9