Reputation: 43
I'm using a spinner to show countries ID. Everything seems fine until I run the app and click on the spinner. After I click on the spinner the app crash.
Here's my spinner xml code (which is inside a LinearLayout, if this is useful):
<Spinner
style="@style/mediumBlackDefault"
android:id="@+id/spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="4" />
And here's how I populate it:
Spinner spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
getApplicationContext(),R.array.countries_array,
android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Also this is the countries_array:
<string-array name="countries_array">
<item>AE</item>
<item>AF</item>
<item>AG</item>
<item>AI</item>
<item>AL</item>
<item>AM</item>
</string-array>
Finally I get these errors:
03-09 11:39:36.944: E/AndroidRuntime(990): FATAL EXCEPTION: main
03-09 11:39:36.944: E/AndroidRuntime(990): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
03-09 11:39:36.944: E/AndroidRuntime(990): at android.view.ViewRootImpl.setView(ViewRootImpl.java:519)
03-09 11:39:36.944: E/AndroidRuntime(990): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:279)
03-09 11:39:36.944: E/AndroidRuntime(990): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:193)
03-09 11:39:36.944: E/AndroidRuntime(990): at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:118)
03-09 11:39:36.944: E/AndroidRuntime(990): at android.app.Dialog.show(Dialog.java:274)
03-09 11:39:36.944: E/AndroidRuntime(990): at android.app.AlertDialog$Builder.show(AlertDialog.java:932)
03-09 11:39:36.944: E/AndroidRuntime(990): at android.widget.Spinner$DialogPopup.show(Spinner.java:672)
03-09 11:39:36.944: E/AndroidRuntime(990): at android.widget.Spinner.performClick(Spinner.java:435)
03-09 11:39:36.944: E/AndroidRuntime(990): at android.view.View$PerformClick.run(View.java:13983)
03-09 11:39:36.944: E/AndroidRuntime(990): at android.os.Handler.handleCallback(Handler.java:605)
03-09 11:39:36.944: E/AndroidRuntime(990): at android.os.Handler.dispatchMessage(Handler.java:92)
03-09 11:39:36.944: E/AndroidRuntime(990): at android.os.Looper.loop(Looper.java:137)
03-09 11:39:36.944: E/AndroidRuntime(990): at android.app.ActivityThread.main(ActivityThread.java:4340)
03-09 11:39:36.944: E/AndroidRuntime(990): at java.lang.reflect.Method.invokeNative(Native Method)
03-09 11:39:36.944: E/AndroidRuntime(990): at java.lang.reflect.Method.invoke(Method.java:511)
03-09 11:39:36.944: E/AndroidRuntime(990): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
03-09 11:39:36.944: E/AndroidRuntime(990): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
03-09 11:39:36.944: E/AndroidRuntime(990): at dalvik.system.NativeStart.main(Native Method)
I've read all the posts available and none solved my problem.
Many thanks in advance!
Upvotes: 4
Views: 4955
Reputation: 37729
I would suggest you to try passing YourActivityName.this
instead of getApplicationContext()
as Context
to your adapter.
Are you using ActivityGroup
or simple Activity
?
If you are using ActivityGroup
, then it may cause problem of bad window token. Following post address the issue with ActivityGroup
Upvotes: 4
Reputation: 33996
User the parent activity's context instead of using the current activity Change the line like below
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
getParent(), R.array.countries_array,
android.R.layout.simple_spinner_item);
Also you can refer this link
Upvotes: 0
Reputation: 54322
It is because of your context,
Instead of getApplicationContext()
use your Activity's context using this reference
Eg.
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
activity.this,R.array.countries_array,
android.R.layout.simple_spinner_item);
or if you are using Activity Groups, provide your Activity Group's Context.
Eg.
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
activity.group,R.array.countries_array,
android.R.layout.simple_spinner_item);
Upvotes: 3