可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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)
回答1:
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).
回答2:
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);
回答3:
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.
回答4:
Make sure that you type: Toast toast = Toast.makeText(this, text, duration);
Not: Toast toast = new Toast.makeText(this, text, duration);
回答5:
Try Toast toast = Toast.makeText(getActivity(), text, duration);
You may also wish to append .show()
if you want it to display
回答6:
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(); }
```
回答7:
Toast.makeText(YourActivity.this, text, duration).show();
回答8:
If you are trying to Toast
your text in the MainActivity then do this:
Toast.makeText(getApplicationContext(), "Your text", Toast.LENGTH_LONG).show();
回答9:
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:)
回答10:
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();
回答11:
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();