Reputation: 9394
I have the following crash
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)
can anyone please help and tell me what cause this crash and how to solve it ?
EDIT
this is the method that show the dialog
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();
}
}
Upvotes: 1
Views: 2572
Reputation: 23881
The error occurs when Activity
gets finished before dialog successfully dismisses. So the Dialog's view is not attached to the windowManager
.
Add this check before dismissing the Dialog:
if (!activity.this.isFinishing() && dialog != null) {
dialog.dismiss();
}
Alternatively you can dismiss your dialog in onPause()
or onDestroy()
of the activity.
@Override
public void onPause() {
super.onPause();
if ((dialog != null) && dialog.isShowing())
dialog.dismiss();
dialog = null;
}
Upvotes: 5