Cannot resolve maketext() method of Toast

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

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

    ```

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

    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:21
    Toast.makeText(YourActivity.this, text, duration).show();
    
    0 讨论(0)
  • 2021-02-07 02:23

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

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

    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)
提交回复
热议问题