android - 非法状态异常 : view has already been added to the window manager

标签 android android-toast

我花了几个小时试图修复应用程序崩溃,我认为这值得提问:

异常:

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)

代码:

我在这个 guide 之后有一个自定义 toast

我将自定义 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);

这样我就可以用showMessage()了在继承的所有 Activity 中MyBaseActivity .

当我调用 toast.show(); 时似乎发生了异常但它不会一直发生(只有极少数情况)我仍然不知道是什么导致了异常。

最佳答案

首先 - 当您就异常寻求帮助时,请始终向我们提供完整的堆栈跟踪信息。

你搞砸了通货膨胀和 toast 的使用。当你第一次使用布局时,你会很酷。第二次,它已经有一个父对象(Toast),所以当您尝试通过 setView 将它添加到 toast 时它会抛出错误。您要么需要为每个 Toast 扩充一个新副本,要么在将其添加到新 Toast 之前将其从旧 Toast 中删除。

关于android - 非法状态异常 : view has already been added to the window manager,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28957174/

相关文章:

android - 杀死安卓 toast ?

android - EditText.setSelection(end) 没有效果?

android - 调用 camera.release() 时手机死机

java - (代码审查)涉及逻辑和(&&)运算符的 Java if 语句

java - toast 消息应该显示与触发的次数一样多的次数,但是它只显示一次

android - 是否可以禁用 toast 或等到 toast 在测试时消失

java - 根据 ID 读取实际的 Firebase 记录

android - 创建应用程序时如何将电话号码链接到图像?

android - toast.getView() 和 toast.setView() 已弃用

Android - 在 ListFragment 中显示自定义 Toast 会引发错误