我正在尝试按顺序运行toast以显示ruuning rss feed。运行时出现以下错误:java.lang.RuntimeException:此Toast未使用Toast.makeText()创建
我的代码是:
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();
}
答案 0 :(得分:18)
要将文字设置为toast,您必须通过makeText
对其进行初始化。
像这样:
Toast toast = Toast.makeText(this, "message", Toast.LENGTH_SHORT);
toast.setText("new message");
toast.setGravity(Gravity.CENTER, 0, 0);
//other setters
toast.show();
答案 1 :(得分:4)
如果您之前使用 makeText 方法之一创建了吐司,则只能调用 toast.SetText()。请参阅该方法的文档: http://developer.android.com/reference/android/widget/Toast.html#setView(android.view.View)
在您的示例中,您应该使用 TextView 而不是 Toast
更新文本答案 2 :(得分:2)
Toast U可以像这样指定......
Toast.makeText(getApplicationContext(), "hai", Toast.LENGTH_LONG).show();
然后你可以这样写......
String s=episode_titles.get(i).toString();
Toast.makeText(getApplicationContext(), "UrMessage:"+s, Toast.LENGTH_LONG).show();
答案 3 :(得分:2)
您可以使用此
for (int i=0;i<episode_titles.size();i++)
{
Toast.makeText(getApplicationContext(), episode_titles.get(i).toString(), Toast.LENGTH_LONG).show();
}
答案 4 :(得分:1)
Toast.makeText(getApplicationContext(), "your text", Toast.LENGTH_LONG).show();
它适用于我。
答案 5 :(得分:1)
而不是toast.setText(episode_titles.get(i).toString());
,请使用
text.setText();
。
答案 6 :(得分:0)
试试这个:
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();
}
让我知道这是否有效:)
答案 7 :(得分:0)
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();
}
答案 8 :(得分:0)
要将文本设置为toast,必须使用makeText。
赞:
Toast toast = Toast.makeText (MainActivity.this,getString(R.string.noSearchMatch ) , LENGTH_SHORT );
toast.setGravity ( Gravity.CENTER_VERTICAL, 0 , 0);
toast.show ( );