2
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)

With no stacktrace line referring to a line in my app. I read up on this and found a reference to this problem here: Android 1.6: "android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application"

They recommend using this instead of getApplicationContext() when calling the AlertDialog. Here is how I am calling the Spinner:

mSpinner = (Spinner)layout.findViewById(R.id.s_freqs);
ArrayAdapter<CharSequence> 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<?> parent, View v, int position, long id) {
        SharedPreferences.Editor editor = mPrefs.edit();
        editor.putInt("update", position);
        editor.commit();
        startOrEditAlarm();
    }

    @Override
    public void onNothingSelected(AdapterView<?> arg0) {

    }

};

mSpinner.setOnItemSelectedListener(spinnerListener);

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:

ArrayAdapter<CharSequence> alpha = ArrayAdapter.createFromResource(getApplicationContext(), R.array.update_freqs, android.R.layout.simple_spinner_item);

ArrayAdapter<CharSequence> alpha = ArrayAdapter.createFromResource(mContext, R.array.update_freqs, android.R.layout.simple_spinner_item); //context member var

ArrayAdapter<CharSequence> alpha = ArrayAdapter.createFromResource(this, R.array.update_freqs, android.R.layout.simple_spinner_item); //activity member var

Nothing works, always the same exception I posted above.

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:

layout = (LinearLayout) inflater.inflate(R.layout.settings, null, false);
initSettingsLayout(layout);

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.

Any input? Thank you!

EDIT: I tried removing the code of the onItemSelected() method. I changed it to:

@Override
public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
    Log.e("test test", "test");
}

I get the same stack with that log line preceding it.

Community
  • 1
  • 1
JMRboosties
  • 14,480
  • 18
  • 68
  • 113

1 Answers1

2

I think that @Roy was on the right track with his question about the Inflater. I was using the LayoutInflater.from(..) method myself, and passing in the getApplicationContext(). Switching to "this" fixed my issue completely. YMMV.

One other side note, for me anyway, this was only causing a problem on some Android devices and not all of them. In fact, my Galaxy Nexus running ICS was fine while many others were not.

Josh
  • 1,316
  • 11
  • 19