Android 显示toast时出错
为了显示一个ruuning rss提要,我正在尝试按顺序运行一个toast。我在运行时遇到以下错误:java.lang.RuntimeException:此Toast不是使用Toast.makeText()创建的 我的代码是:Android 显示toast时出错,android,toast,Android,Toast,为了显示一个ruuning rss提要,我正在尝试按顺序运行一个toast。我在运行时遇到以下错误:java.lang.RuntimeException:此Toast不是使用Toast.makeText()创建的 我的代码是: LayoutInflater inflater = getLayoutInflater(); View layout = inflater.inflate(R.layout.toast_layout, (Vie
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();
}
LayoutInflater充气机=getLayoutInflater();
视图布局=充气机。充气(R.layout.toast_布局,
(视图组)findViewById(R.id.toast_layout_root));
ImageView image=(ImageView)layout.findViewById(R.id.toAssembage);
image.setImageResource(R.drawable.bball_图标);
TextView text=(TextView)layout.findViewById(R.id.toasttext);
Toast Toast=新Toast(getApplicationContext());
toast.setView(布局);
对于(inti=0;i土司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();
然后你可以这样写
Toast.makeText(getApplicationContext(), "hai", Toast.LENGTH_LONG).show();
String s=episode_titles.get(i).toString();
Toast.makeText(getApplicationContext(), "UrMessage:"+s, Toast.LENGTH_LONG).show();
它对我很有用。如果您以前使用makeText方法之一创建了toast,则只能调用toast.SetText()。请参阅该方法的文档:
)
在您的示例中,应该使用TextView而不是Toast更新文本
for (int i=0;i<episode_titles.size();i++)
{
Toast.makeText(getApplicationContext(), episode_titles.get(i).toString(), Toast.LENGTH_LONG).show();
}
for(int i=0;i请尝试以下方法:
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();
}
LayoutInflater充气机=getLayoutInflater();
视图布局=充气机。充气(R.layout.toast_布局,
(视图组)findViewById(R.id.toast_layout_root));
ImageView image=(ImageView)layout.findViewById(R.id.toAssembage);
image.setImageResource(R.drawable.bball_图标);
TextView text=(TextView)layout.findViewById(R.id.toasttext);
Toast Toast=新Toast(getApplicationContext());
toast.setView(布局);
对于(int i=0;i要将文本设置为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();
Toast toast = Toast.makeText (MainActivity.this,getString(R.string.noSearchMatch ) , LENGTH_SHORT );
toast.setGravity ( Gravity.CENTER_VERTICAL, 0 , 0);
toast.show ( );
使用
text.setText();
要将文本设置为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();
Toast toast = Toast.makeText (MainActivity.this,getString(R.string.noSearchMatch ) , LENGTH_SHORT );
toast.setGravity ( Gravity.CENTER_VERTICAL, 0 , 0);
toast.show ( );
你能发布你的错误的日志吗?02-28 11:11:10.421:E/AndroidRuntime(9715):java.lang.RuntimeException:这个Toast不是用Toast.makeText()创建的实际上我们需要引用实际错误的日志,你提到了在android.widget.Toast.setText(Toast 272)上运行:java.lang.RuntimeException你能在创建插曲标题的地方发布代码吗?试过这个:“text.setText(插曲标题.get(i).toString());”但只得到插曲标题的最后一个结果。无法直接添加文本,@Andhrudu是的。请看我的回答。