title: Android: Spinner throwing Unable to add window -- token null is not for an application exception
body: <pre><code>03-02 13:33:40.296: E/AndroidRuntime(525): FATAL EXCEPTION: main
  03-02 13:33:40.296: E/AndroidRuntime(525): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
  03-02 13:33:40.296: E/AndroidRuntime(525):  at android.view.ViewRoot.setView(ViewRoot.java:531)
  03-02 13:33:40.296: E/AndroidRuntime(525):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
  03-02 13:33:40.296: E/AndroidRuntime(525):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
  03-02 13:33:40.296: E/AndroidRuntime(525):  at android.app.Dialog.show(Dialog.java:241)
  03-02 13:33:40.296: E/AndroidRuntime(525):  at android.app.AlertDialog$Builder.show(AlertDialog.java:802)
  03-02 13:33:40.296: E/AndroidRuntime(525):  at android.widget.Spinner.performClick(Spinner.java:260)
  03-02 13:33:40.296: E/AndroidRuntime(525):  at android.view.View$PerformClick.run(View.java:9080)
  03-02 13:33:40.296: E/AndroidRuntime(525):  at android.os.Handler.handleCallback(Handler.java:587)
  03-02 13:33:40.296: E/AndroidRuntime(525):  at android.os.Handler.dispatchMessage(Handler.java:92)
  03-02 13:33:40.296: E/AndroidRuntime(525):  at android.os.Looper.loop(Looper.java:123)
  03-02 13:33:40.296: E/AndroidRuntime(525):  at android.app.ActivityThread.main(ActivityThread.java:3683)
  03-02 13:33:40.296: E/AndroidRuntime(525):  at java.lang.reflect.Method.invokeNative(Native Method)
  03-02 13:33:40.296: E/AndroidRuntime(525):  at java.lang.reflect.Method.invoke(Method.java:507)
  03-02 13:33:40.296: E/AndroidRuntime(525):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
  03-02 13:33:40.296: E/AndroidRuntime(525):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
  03-02 13:33:40.296: E/AndroidRuntime(525):  at dalvik.system.NativeStart.main(Native Method)
  </code></pre>
  
  <p>With no stacktrace line referring to a line in my app. I read up on this and found a reference to this problem here: <a href="https://stackoverflow.com/questions/2634991/android-1-6-android-view-windowmanagerbadtokenexception-unable-to-add-window">Android 1.6: &quot;android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application&quot;</a></p>
  
  <p>They recommend using <code>this</code> instead of getApplicationContext() when calling the AlertDialog. Here is how I am calling the Spinner:</p>
  
  <pre><code>mSpinner = (Spinner)layout.findViewById(R.id.s_freqs);
  ArrayAdapter&lt;CharSequence&gt; alpha = ArrayAdapter.createFromResource(this, R.array.update_freqs, android.R.layout.simple_spinner_item); //Line in question I'm guessing.
  alpha.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
  mSpinner.setAdapter(alpha);
  AdapterView.OnItemSelectedListener spinnerListener = new AdapterView.OnItemSelectedListener() {
  
      @Override
      public void onItemSelected(AdapterView&lt;?&gt; parent, View v, int position, long id) {
          SharedPreferences.Editor editor = mPrefs.edit();
          editor.putInt("update", position);
          editor.commit();
          startOrEditAlarm();
      }
  
      @Override
      public void onNothingSelected(AdapterView&lt;?&gt; arg0) {
  
      }
  
  };
  
  mSpinner.setOnItemSelectedListener(spinnerListener);
  </code></pre>
  
  <p>One thing that could be causing a problem here is that the method where this code resides is called by a ViewPager's instatiateItem() method. I tried doing the following changes to that line in question:</p>
  
  <pre><code>ArrayAdapter&lt;CharSequence&gt; alpha = ArrayAdapter.createFromResource(getApplicationContext(), R.array.update_freqs, android.R.layout.simple_spinner_item);
  
  ArrayAdapter&lt;CharSequence&gt; alpha = ArrayAdapter.createFromResource(mContext, R.array.update_freqs, android.R.layout.simple_spinner_item); //context member var
  
  ArrayAdapter&lt;CharSequence&gt; alpha = ArrayAdapter.createFromResource(this, R.array.update_freqs, android.R.layout.simple_spinner_item); //activity member var
  </code></pre>
  
  <p>Nothing works, always the same exception I posted above.</p>
  
  <p>Like I said above, I think the problem lies in the way this layout is built. I am using a ViewPager, so the instatiateItem() method in that ViewPager calls the following code to launch this view:</p>
  
  <pre><code>layout = (LinearLayout) inflater.inflate(R.layout.settings, null, false);
  initSettingsLayout(layout);
  </code></pre>
  
  <p>This works fine in all other aspects but this. It is also worth noting that I call a AlertDialog in this same ViewPager and it does not misbehave.</p>
  
  <p>Any input? Thank you!</p>
  
  <p>EDIT: I tried removing the code of the onItemSelected() method. I changed it to:</p>
  
  <pre><code>@Override
  public void onItemSelected(AdapterView&lt;?&gt; parent, View v, int position, long id) {
      Log.e("test test", "test");
  }
  </code></pre>
  
  <p>I get the same stack with that log line preceding it.</p>