gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:08:01 26 4
gpt4 key购买 nike

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

异常:

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com