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);
It's obvious from the error message that the problem is with context used for creating the spinner. Try this
viewToLoad = LayoutInflater.from(this).inflate(R.layout.line_discount, null);
Or:
viewToLoad = getLayoutInflater().inflate(R.layout.line_discount, null);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, proList);
sp.setAdapter(adapter);
When you create your ArrayAdapter
you should pass the Context
of your ActivityGroup
not the Context
of your current Activity
.
Here is an example of how I get it:
public class MyActivityGroup extends ActivityGroup{
pulbic static MyActivityGroup sGroup;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sGroup=this;
//...
}
}
// Tab Implementation
//.....
ArrayAdapter<String> adapter = new ArrayAdapter<String> (
MyActivityGroup.sGroup, android.R.layout.simple_spinner_item, proList);