Toast使用注意--解决错误:The application ** has stopped unexpectedly.Please try again
运行应用的时候出现了The application ** has stopped unexpectedly.Please ?try again 的错误,如下图:

?
通过debug发现异常信息为:
?
Caused by: java.lang.RuntimeException: This Toast was not created with Toast.makeText()
?? ?at android.widget.Toast.setText(Toast.java:275)
?? ?at cn.and.ToastActivity.onCreate(ToastActivity.java:20)
?? ?at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
?? ?at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
?
分析andriod源码发现:
?
/** * 显示Toast * @param toastTxt 提示文本内容 */private void showToast(String toastTxt) {Toast t = new Toast(this);t.setDuration(Toast.LENGTH_SHORT);LinearLayout layout = new LinearLayout(this);TextView textView = new TextView(this);textView.setText(toastTxt);//textView.setTextSize(16);layout.addView(textView);t.setView(layout);t.show();} 1 楼 helwens 2012-05-25 好文章!真想转载