我遇到了以下崩溃
java.lang.IllegalArgumentException: View=DecorView@62c59a[] not attached to window manager
at android.view.WindowManagerGlobal.findViewLocked(WindowManagerGlobal.java:473)
at android.view.WindowManagerGlobal.removeView(WindowManagerGlobal.java:382)
at android.view.WindowManagerImpl.removeViewImmediate(WindowManagerImpl.java:128)
at android.app.Dialog.dismissDialog(Dialog.java:727)
at android.app.Dialog.-android_app_Dialog-mthref-0(Dialog.java:167)
at android.app.Dialog$-void__init__android_content_Context_context_int_themeResId_boolean_createContextThemeWrapper_LambdaImpl0.run(Dialog.java)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6688)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)
任何人都可以帮忙告诉我这次崩溃的原因以及如何解决?
修改
这是显示对话框的方法
public static void showDialogMessage(Activity activity, String title,
String message, String buttonString, final OnClickListener listener) {
try {
final Dialog dialog = new Dialog(activity);
dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.dialog_error_message_layout);
dialog.setCancelable(false);
TextView titleTV = (TextView) dialog.findViewById(R.id.title_textview);
TextView descriptionTV = (TextView) dialog
.findViewById(R.id.description_textview);
Button okButton = (Button) dialog.findViewById(R.id.ok_button);
Button cancelButton = (Button) dialog.findViewById(R.id.cancel_button);
titleTV.setText(title);
descriptionTV.setText(message);
okButton.setText(buttonString);
cancelButton.setVisibility(View.GONE);
okButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
if (listener != null)
listener.onClick(null);
}
});
dialog.getWindow().setBackgroundDrawableResource(
android.R.color.transparent);
dialog.show();
} catch (BadTokenException exception) {
exception.printStackTrace();
}
}
答案 0 :(得分:1)
在对话框成功解除之前Activity
完成时会发生错误。因此,对话框的视图未附加到windowManager
。
在解除对话框之前添加此检查:
if (!activity.this.isFinishing() && dialog != null) {
dialog.dismiss();
}
或者,您可以在活动的onPause()
或onDestroy()
中关闭对话框。
@Override
public void onPause() {
super.onPause();
if ((dialog != null) && dialog.isShowing())
dialog.dismiss();
dialog = null;
}