error when displaying toast

user1163234 picture user1163234 · Feb 28, 2012 · Viewed 11.5k times · Source

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

}

Answer

sandalone picture sandalone · Sep 4, 2013

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