Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

error when displaying toast

Tags:

android

toast

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

}
like image 907
user1163234 Avatar asked Feb 28 '12 09:02

user1163234


People also ask

How do you show errors in toast?

So basically, Toast. makeText(getBaseContext(),"Your message", Toast. LENGTH_LONG). show(); will generate the toast message and if you want to it to be displayed for some particular case, then just put the condition inside the if statement.

Why is toast not appearing on makeText?

This is what I learnt as things to remember when showing a toast while debugging this issue : Make sure not to forget to call show() after the makeText. Check for the Context , if its the right one. The most important one , make sure your Android Notifications are on for your app, else the Toast will not be shown.

What is a toast message?

An Example When you send messages from the Gmail application, a toast message appears saying “Sending…”. Once the email is sent, the Toast message changes to “Message sent.” The image below is a screen grab of a Toast message in the Gmail Android application.


1 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();
like image 174
sandalone Avatar answered Oct 13 '22 15:10

sandalone