Error displaying toast

I am trying to run a toast in sequence to display a Russian rss feed. I get the following error on startup: java.lang.RuntimeException: this toast was not created using 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(); } 
+8
android toast
source share
9 answers

To specify the text for the toast, you must initialize it with 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(); 
+18
source share

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

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

+4
source share

Toast U can indicate how ...

  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(); 
+2
source share

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(); } 
+2
source share
 Toast.makeText(getApplicationContext(), "your text", Toast.LENGTH_LONG).show(); 

His work is for me.

+1
source share

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

+1
source share

Try the following:

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

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

To set the text for the toast, you must 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 ( ); 
0
source share

All Articles