Dialog throwing "Unable to add window — token null is not for an application” with getApplication() as context

Instead of getApplicationContext(), just use ActivityName.this.


Using this did not work for me, but MyActivityName.this did. Hope this helps anyone who could not get this to work.


You can continue to use getApplicationContext(), but before use, you should add this flag: dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT), and the error will not show.

Add the following permission to your manifest:

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />