Home » Android » android – error when displaying toast

android – error when displaying toast

Posted by: admin June 15, 2020 Leave a comment

Questions:

I am trying to run a toast in sequence in order to display a ruuning rss feed. I am getting the following error when running:java.lang.RuntimeException: This Toast was not created with Toast.makeText()

My code is:

LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.toast_layout,
                               (ViewGroup) findViewById(R.id.toast_layout_root));

ImageView image = (ImageView) layout.findViewById(R.id.toastimage);
image.setImageResource(R.drawable.bball_icon);
TextView text = (TextView) layout.findViewById(R.id.toasttext);

Toast toast = new Toast(getApplicationContext());
toast.setView(layout);
for (int i=0;i<episode_titles.size();i++)
{
    toast.setText(episode_titles.get(i).toString());
    toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
    toast.setDuration(Toast.LENGTH_SHORT);

    toast.show();

}
How to&Answers:

To set text to toast, you have to initialize it via makeText.

Like this:

    Toast toast = Toast.makeText(this, "message", Toast.LENGTH_SHORT);
    toast.setText("new message");
    toast.setGravity(Gravity.CENTER, 0, 0);
    //other setters
    toast.show();

Answer:

You can only call toast.SetText() if you have previously created the toast using one of the makeText methods. See the documentation of the method:
http://developer.android.com/reference/android/widget/Toast.html#setView(android.view.View)

In your example, you should update the text using the TextView rather than the Toast

Answer:

You can use this

for (int i=0;i<episode_titles.size();i++)
{
    Toast.makeText(getApplicationContext(), episode_titles.get(i).toString(), Toast.LENGTH_LONG).show();
}

Answer:

The Toast U can specify like this…

  Toast.makeText(getApplicationContext(), "hai", Toast.LENGTH_LONG).show();

Then u can write like this…

       String s=episode_titles.get(i).toString();
      Toast.makeText(getApplicationContext(), "UrMessage:"+s, Toast.LENGTH_LONG).show();

Answer:

Toast.makeText(getApplicationContext(), "your text", Toast.LENGTH_LONG).show();  

Its works for me.

Answer:

Instead of toast.setText(episode_titles.get(i).toString());, use
text.setText();.

Answer:

Try this out:

LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.toast_layout,
                               (ViewGroup) findViewById(R.id.toast_layout_root));

ImageView image = (ImageView) layout.findViewById(R.id.toastimage);
image.setImageResource(R.drawable.bball_icon);
TextView text = (TextView) layout.findViewById(R.id.toasttext);

Toast toast = new Toast(getApplicationContext());
toast.setView(layout);
for (int i=0;i<episode_titles.size();i++)
{
    text.setText(episode_titles.get(i).toString());
    toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
    toast.setDuration(Toast.LENGTH_SHORT);

    toast.show();

}

Let me know if this works 🙂

Answer:

Toast toast = new Toast(getApplicationContext());
//your for loop here    {   
        toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
        toast.setDuration(Toast.LENGTH_LONG);
        TextView txt1 =  new TextView(this);
        txt1.setText(R.string.hello);
        toast.setView(txt1);
        toast.show();
    }

Answer:

To set text to toast , you have to use makeText.

Like this:

Toast toast =  Toast.makeText (MainActivity.this,getString(R.string.noSearchMatch ) , LENGTH_SHORT );
      toast.setGravity ( Gravity.CENTER_VERTICAL, 0 , 0);
      toast.show ( );