android-toast - IllegalStateException: 视图已经添加到窗口管理器中

我花了几个小时来修复应用程序崩溃,它应该是一个问题:

例外:


java.lang.IllegalStateException: View android.widget.LinearLayout{41a97eb8 V.E..... ......ID 0,0-540,105 #7f0b020d app:id/toast_layout_root} has already been added to the window manager.
 at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:223)
 at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
 at android.widget.Toast$TN.handleShow(Toast.java:402)
 at android.widget.Toast$TN$1.run(Toast.java:310)
 at android.os.Handler.handleCallback(Handler.java:730)
 at android.os.Handler.dispatchMessage(Handler.java:92)
 at android.os.Looper.loop(Looper.java:137)
 at android.app.ActivityThread.main(ActivityThread.java:5136)
 at java.lang.reflect.Method.invokeNative(Method.java)
 at java.lang.reflect.Method.invoke(Method.java:525)
 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
 at dalvik.system.NativeStart.main(NativeStart.java)

我定义自定义toast为ToastMessageBar.java,构造器的外观如下所示:


public ToastMessageBar(Activity activity) {
 LayoutInflater inflater = activity.getLayoutInflater;
 mToastLayout = inflater.inflate(R.layout.toast_layout,
 (ViewGroup) activity.findViewById(R.id.toast_layout_root));

 mMessageView = (TextView) mToastLayout.findViewById(R.id.toast_message);
 mSubtitleView = (TextView) mToastLayout.findViewById(R.id.toast_subtitle);
}

我展示一条toast消息的方式如下:


private void showMessage(MessageType type, String message, String subtitle) {
 int duration = Toast.LENGTH_SHORT;
 if (mToastLayout != null) {
 int colorId;
 switch (type) {
 case Warning:
 colorId = R.color.warning_bar_color;
 duration = Toast.LENGTH_SHORT;
 break;

 case Error:
 colorId = R.color.error_bar_color;
 break;

 default:
 colorId = R.color.info_bar_color;
 break;
 }

 mToastLayout.setBackgroundColor(
 MyApp.getContext().getResources().getColor(colorId));

 if (subtitle == null) {
 mMessageView.setVisibility(View.GONE);
 mSubtitleView.setText(message);
 } else {
 mMessageView.setVisibility(View.VISIBLE);
 mMessageView.setText(message);
 mSubtitleView.setText(subtitle);
 }
 }

 Utils.showToast(mToastLayout, message, duration);
}

public static void showToast(View layout, String message, int duration) {
 if (layout != null) {
 Toast toast = new Toast(MyApp.getContext());
 toast.setGravity(Gravity.TOP|Gravity.FILL_HORIZONTAL, 0, 0);
 toast.setDuration(duration);
 toast.setView(layout);
 toast.show();
 return;
 }

 Toast.makeText(MyApp.getContext(), message, Toast.LENGTH_LONG).show();
}

MyBaseActivity.onCreate()中,我定义了ToastMessageBar :


mMessageBar = new ToastMessageBar(this);

这样,我就可以在所有继承MyBaseActivity的活动中使用showMessage()

时间:

首先,当你请求关于异常的帮助时,总是给我们完整的堆栈跟踪。

你在screwing你的通货膨胀和Toast的用法。 当你第一次使用布局时,你很酷。 第二次,它已经有一个父( ),所以当你尝试通过setView将它添加到Toast时,它将抛出一个错误。 你需要每次烤一份新的副本,或者把它从旧杯子中删除,然后再添加到新的。

我认为是这一行的问题


 mToastLayout = inflater.inflate(R.layout.toast_layout,
 (ViewGroup) activity.findViewById(R.id.toast_layout_root));

尝试:


mToastLayout = inflater.inflate(R.layout.toast_layout,null);

...