我有以下崩溃
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)
Run Code Online (Sandbox Code Playgroud)
任何人都可以请帮助并告诉我是什么原因导致此崩溃以及如何解决它?
编辑
这是显示对话框的方法
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();
}
}
Run Code Online (Sandbox Code Playgroud)
Activity在对话框成功关闭之前完成时发生错误。所以对话框的视图没有附加到windowManager.
在关闭对话框之前添加此检查:
if (!activity.this.isFinishing() && dialog != null) {
dialog.dismiss();
}
Run Code Online (Sandbox Code Playgroud)
或者,您可以关闭活动中onPause()或onDestroy()活动中的对话。
@Override
public void onPause() {
super.onPause();
if ((dialog != null) && dialog.isShowing())
dialog.dismiss();
dialog = null;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1783 次 |
| 最近记录: |