Cannot resolve maketext() method of Toast

后端 未结 18 2409
旧时难觅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:17

    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)
  • 2021-02-07 02:18

    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();
    
    0 讨论(0)
  • 2021-02-07 02:20

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

    If you are trying to Toast your text in the MainActivity then do this:

    Toast.makeText(getApplicationContext(), "Your text", Toast.LENGTH_LONG).show();
    
    0 讨论(0)
  • 2021-02-07 02:20

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

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

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

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

    0 讨论(0)
提交回复
热议问题