4

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!

tcp
  • 43
  • 1
  • 3
  • Thanks for all the answers! I had to change all getApplicationContext() to **this** for it to work – tcp Mar 09 '12 at 15:52

3 Answers3

4

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

Community
  • 1
  • 1
Adil Soomro
  • 37,609
  • 9
  • 103
  • 153
  • 1
    Cheers for the reply. I just had to change all the getApplicationContext() to this! – tcp Mar 09 '12 at 15:54
  • This answer is not working for me. I have spinner inside an activity which is included in a tab activity group. what should i do – Pramod J George Jan 23 '14 at 05:37
  • @PramodJGeorge you should try using `this.getParent()`, have a look at this post: [Spinner within Child of ActivityGroup doesn't work](http://stackoverflow.com/a/6611796/593709) and this: [Error while placing a spinner inside Activity Group](http://stackoverflow.com/a/4644485/593709) – Adil Soomro Jan 23 '14 at 06:59
  • @AdilSoomro thanks for this comment. I found this solution few hours before. – Pramod J George Jan 23 '14 at 08:53
3

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);
Andro Selva
  • 53,910
  • 52
  • 193
  • 240
  • Cheers for the reply. I just had to change all the getApplicationContext() to this! – tcp Mar 09 '12 at 15:54
0

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

Community
  • 1
  • 1
Dharmendra
  • 33,296
  • 22
  • 86
  • 129