Cannot resolve maketext() method of Toast

后端 未结 18 1138
南笙
南笙 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:25

    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.

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

    in the onClick method try this

    
    Toast.makeText(view.getContext(), "sorry", Toast.LENGTH_LONG).show();
    
    
    

    it did work form me.

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

    Try Toast toast = Toast.makeText(getActivity(), text, duration);

    You may also wish to append .show() if you want it to display

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

    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:34

    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:35

    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;
    }
    
    0 讨论(0)
提交回复
热议问题