Dialog throwing Unable to add window token null is not for an application with getApplication as context

0 votes

My Activity is trying to create an AlertDialog which requires a Context as a parameter. This works as expected if I use:

AlertDialog.Builder builder = new AlertDialog.Builder(this);

However, I am leery of using "this" as a context due to the potential for memory leaks when Activity is destroyed and recreated even during something simple like a screen rotation. "Unable to add window — token null is not for an application”

So, should this really be considered a "bug", since we are officially advised to use Activity.getApplication() and yet it doesn't function as advertised?

Feb 17, 2022 in Others by Rahul
• 9,680 points
2,184 views

1 answer to this question.

0 votes
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" />

answered Feb 17, 2022 by Aditya
• 7,680 points

Related Questions In Others

0 votes
0 answers

Unable to add window -- token null is not valid; is your activity running?

I am trying to put a feature ...READ MORE

May 18, 2022 in Others by Kichu
• 19,040 points
6,940 views
0 votes
1 answer

'git rev-parse HEAD' is not recognized as an internal or external command

Hi@akhtar, I also faced the same problem. You ...READ MORE

answered Jul 17, 2020 in Others by MD
• 95,460 points
4,095 views
0 votes
1 answer

How do I display an alert dialog on Android?

You could use an AlertDialog for this ...READ MORE

answered Feb 22, 2022 in Others by Aditya
• 7,680 points
1,446 views
0 votes
1 answer

Running docker on Android

According to the documentation, the Android kernel is ...READ MORE

answered Aug 1, 2018 in Docker by Kalgi
• 52,350 points
3,763 views
0 votes
1 answer

Task Canceled Exception while invoking AWS Lambda

I'm guessing either the TaskCanceledException instance is ...READ MORE

answered Sep 19, 2018 in AWS by Priyaj
• 58,020 points
2,460 views
0 votes
1 answer

Is there a way to run Python on Android?

YES! here’s a barcode scanner written in six ...READ MORE

answered Sep 19, 2018 in Python by Priyaj
• 58,020 points
1,018 views
0 votes
1 answer

'adb' is not recognized as an internal or external command, operable program or batch file

Set the path of adb into System ...READ MORE

answered Feb 16, 2022 in Others by Aditya
• 7,680 points
7,129 views
0 votes
1 answer

'composer' is not recognized as an internal or external command in windows server

For anyone coming here from Google who ...READ MORE

answered Feb 17, 2022 in Others by Aditya
• 7,680 points
20,399 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP