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

Leave a Comment