Cannot resolve maketext() method of Toast

后端 未结 18 2388
旧时难觅i
旧时难觅i 2021-02-07 02:02

I am getting error while creating a Toast

Toast toast = Toast.makeText(this, text, duration);

I am getting cannot resolve ma

相关标签:
18条回答
  • 2021-02-07 02:24

    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.

    0 讨论(0)
  • 2021-02-07 02:29

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

    0 讨论(0)
  • 2021-02-07 02:29

    First add

     import android.widget.Toast;
    

    statement if you did not already and then

    Toast.makeText(YourActvityName.this, "Your Text", Toast.LENGTH_SHORT).show();
    
    0 讨论(0)
  • 2021-02-07 02:33

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

    0 讨论(0)
  • 2021-02-07 02:33

    Make sure that you type: Toast toast = Toast.makeText(this, text, duration);

    Not: Toast toast = new Toast.makeText(this, text, duration);

    0 讨论(0)
  • 2021-02-07 02:34
    Toast.makeText(YourActivity.this, text, duration).show();
    
    0 讨论(0)
提交回复
热议问题