2

My application crashes with below error stack on specific android version and device (VIVO with android 5.1 & Oneplus one with 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) _*

The code snippet from QuickMath.java as follows

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();

I suspect the initialisation of customDialog. Am I right in using the context as 'this'?

1 Answers1

0

android.view.WindowManager$BadTokenException:

You should call YourActivity.this instead of this.

Don't

final Dialog customDialog = new Dialog(this);

Do

final Dialog customDialog = new Dialog(QuickMath.this);
IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
  • 1
    Please accept my sincere apology for delayed response. I'm still very new to stack overflow. I wasn't sure where the tick sing mark is. :). – Yathiraj J P Nov 27 '17 at 12:37
  • The same issue reported again in new devices now, after making the suggested changes. Any help please? – Yathiraj J P Nov 28 '17 at 15:48
  • Could anybody explain why it is working like that? What is the difference between `this` and `Class.this` ? – Wackaloon Jan 24 '19 at 10:55
  • @AlexanderAgeychenko https://stackoverflow.com/questions/12594229/whats-the-difference-between-passing-this-vs-classname-this-from-an-event-hand – IntelliJ Amiya Jan 24 '19 at 10:58