自定义对话框上的android.view.WindowManager $ BadTokenException

时间:2017-11-09 11:44:20

标签: android exception window-managers customdialog

我的应用程序在特定的Android版本和设备上崩溃了以下错误堆栈(VIVO与Android 5.1& Oneplus与Android 7.1)

*_ android.view.WindowManager$BadTokenException:
  at android.view.ViewRootImpl.setView (ViewRootImpl.java:679)
  at android.view.WindowManagerGlobal.addView (WindowManagerGlobal.java:342)
  at android.view.WindowManagerImpl.addView (WindowManagerImpl.java:94)
  at android.app.Dialog.show (Dialog.java:329)
  at com.yathirajjp.brainstimuli.QuickMath.showCustomDialog (QuickMath.java:193)
  at com.yathirajjp.brainstimuli.QuickMath$3.onFinish (QuickMath.java:252)
  at android.os.CountDownTimer$1.handleMessage (CountDownTimer.java:127)
  at android.os.Handler.dispatchMessage (Handler.java:102)
  at android.os.Looper.loop (Looper.java:154)
  at android.app.ActivityThread.main (ActivityThread.java:6186)
  at java.lang.reflect.Method.invoke (Native Method)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:889)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:779) _*

QuickMath.java的代码片段如下

final Dialog customDialog = new Dialog(**this**);
customDialog.setContentView(R.layout.custom_dialog_layout);

waitTimer.cancel();  // Cancelling the CountDownTimer before calling the show custom dialog

customDialog.show();

我怀疑customDialog的初始化。我是否正确使用上下文作为'这'?

1 个答案:

答案 0 :(得分:0)

  

android.view.WindowManager $ BadTokenException:

您应拨打 YourActivity.this ,而不是 this

<强>唐&#39;吨

final Dialog customDialog = new Dialog(this);

<强>不要

final Dialog customDialog = new Dialog(QuickMath.this);