I created a custom AlertDialog to display a Spinner and some EditText elements. When I select a menu option, this custom AlertDialog gets launched. Everything works fine until I actually select the spinner to try and select an item, at which point I get the BadTokenException. I've read other StackOverflow posts saying that similiar exceptions were the result of trying to display a diaglog using getApplicationContext() rather than passing Activity.this. I'm not explicitly passing getApplicationContext() to anything related to this AlertDialog.
To setup this custom AlertDiaglog, I created a layout file containing the Spinner and EditText elements, and then made the AlertDialog use that layout:
LayoutInflater inflater = (LayoutInflater)getApplication().getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.custom_layout, (ViewGroup)findViewById(R.id.custom_layout_root));
spinner = (Spinner)layout.findViewById(R.id.custom_layout_spinner);
ArrayAdapter adap = ArrayAdapter.createFromResource(Activity.this, R.array.data, android.R.layout.simple_spinner_item);
adap.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adap);
spinner.setOnItemSelectedListener(new SpinnerItemListener());
AlertDialog.Builder dialog = new AlertDialog.Builder(Activity.this);
dialog.setTitle("Title");
dialog.setPositiveButton("Ok", new Dialog.OnClickListener() ...
...
dialog.show();
This code all works fine for displaying the AlertDialog. However, when I actually touch the Spinner, I get the following problem:
Thread [<3> main] (Suspended (exception WindowManager$BadTokenException))
ViewRoot.handleMessage(Message) line: 1704
ViewRoot(Handler).dispatchMessage(Message) line: 99
Looper.loop() line: 123
ActivityThread.main(String[]) line: 4203
Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]
Method.invoke(Object, Object...) line: 521
ZygoteInit$MethodAndArgsCaller.run() line: 791
ZygoteInit.main(String[]) line: 549
NativeStart.main(String[]) line: not available [native method]
Does anyone have an idea of what's going on? The exception message says:
Unable to add window -- token null is not for an application
I believe this is the message others have seen for this type of problem, so I'm assuming that ApplicationContext is somewhere in the mix, but I'm not sure where. My manifest is setup with minSdk of 1.5 with a Target SDK of 1.6.