Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot resolve maketext() method of Toast

Tags:

android

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)
like image 947
Muneem Habib Avatar asked Jul 18 '13 07:07

Muneem Habib


4 Answers

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).

like image 180
Blackbelt Avatar answered Nov 04 '22 12:11

Blackbelt


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);

like image 34
Pushp Raj Saurabh Avatar answered Nov 04 '22 11:11

Pushp Raj Saurabh


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.

like image 9
Scott Helme Avatar answered Nov 04 '22 10:11

Scott Helme


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();
    }

```

like image 7
Shaun Dychko Avatar answered Nov 04 '22 11:11

Shaun Dychko