Android Spinner Error : android.view.WindowManager$BadTokenException: Unable to add window
I think you have context problem.Try to get context using below method
you can create a new activity and set its theme to dialog theme
so that when you start your activity it will display as dialog.
For more information about dialog see below post
Click here
EDIT2
I found a solution for badTokenExcaption
In your activity's onCreate()
method replace the line setContentView(R.layout.XXXXX)
by
View viewToLoad = LayoutInflater.from(this.getParent()).inflate(R.layout.XXXXX, null);
this.setContentView(viewToLoad);
and replace the spinner code by following lines
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource( this, R.array.medicine_types, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
spDosageType.setAdapter(adapter);
android.view.WindowManager$BadTokenException while clicking on android spinner
I found a solution for badTokenExcaption
In your activity's onCreate() method replace the line setContentView(R.layout.XXXXX) by
View viewToLoad = LayoutInflater.from(this.getParent()).inflate(R.layout.XXXXX, null);
this.setContentView(viewToLoad);
and replace the spinner code by following lines
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource( this, R.array.medicine_types, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
spDosageType.setAdapter(adapter);
Bad token exception on spinner which uses (android:entries=@array/type) in fragment
I solved that error by putting my popup
's XML code in fragment and make it visible on click. By that way spinner
's code runs nicely on every device that way error must be somewhere in using spinner in custom popup.
Click on spinner gives WindowManager$BadTokenException
pass
YourActivty.this.getParent()
as Context
to the Spinner
.
For more information see here and here
Related Topics
The Specified Child Already Has a Parent. You Must Call Removeview() on the Child's Parent First
How to Find the Android Version Name Programmatically
How to Store and Retrieve a Byte Array (Image Data) to and from a SQLite Database
My App Was Rejected by Instagram Basic Display API Review Due to Invalid Reasons
How to Solve Nullpointerexception Error in Android
Android 4.2: Back Stack Behaviour with Nested Fragments
Cordova Platform Add Android Not Working While Listing Android Targets
Android: How Handle Message Error from the Server Using Volley
Ndk - How to Use a Generated .So Library in Another Project
How to Distinguish Between Move and Click in Ontouchevent()
Android - Margins Specified in Custom Style Not Taking Effect
Why Is My Field 'Null' After Injection? How to Inject My Object
How to Import Contacts from Phonebook to Our Application
Custom Translucent Android Actionbar
Refreshing Activity on Receiving Gcm Push Notification
Want to Compile Native Android Binary I Can Run in Terminal on the Phone