ActionBarSherlock
ActionBarSherlock copied to clipboard
WindowManager crash - unable to add window
I have problem with action bar menu button when I press it on Android 2.2 (emulator). It sometimes crashes with this exception:
FATAL EXCEPTION: main
android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?
at android.view.ViewRoot.setView(ViewRoot.java:505)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
at android.view.Window$LocalWindowManager.addView(Window.java:424)
at android.widget.PopupWindow.invokePopup(PopupWindow.java:828)
at android.widget.PopupWindow.showAsDropDown(PopupWindow.java:740)
at com.actionbarsherlock.internal.widget.IcsListPopupWindow.show(IcsListPopupWindow.java:226)
at com.actionbarsherlock.internal.view.menu.MenuPopupHelper.tryShow(MenuPopupHelper.java:129)
at com.actionbarsherlock.internal.view.menu.MenuPopupHelper.show(MenuPopupHelper.java:102)
at com.actionbarsherlock.internal.view.menu.ActionMenuPresenter.onSubMenuSelected(ActionMenuPresenter.java:282)
at com.actionbarsherlock.internal.view.menu.MenuBuilder.dispatchSubMenuSelected(MenuBuilder.java:263)
at com.actionbarsherlock.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:892)
at com.actionbarsherlock.internal.view.menu.ActionMenuView.invokeItem(ActionMenuView.java:510)
at com.actionbarsherlock.internal.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:145)
at android.view.View.performClick(View.java:2408)
at android.view.View$PerformClick.run(View.java:8816)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4627)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
at dalvik.system.NativeStart.main(Native Method)
This is the source code:
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
// ...
case R.id.ab_button_pick:
Intent intentPick = new Intent(this, PickActivity.class);
startActivity(intentPick);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Menu layout:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/ab_button_pick"
android:title="@string/ab_button_pick"
android:icon="@drawable/ic_menu_pick"
android:showAsAction="ifRoom"
android:orderInCategory="2" />
</menu>
Any idea why this is happening? Thanks.
android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?
Did you declare it in AndroidMainfest.xml?
Yes, I did. On Jelly Bean, it works fine.
Get this on Android 2.3 works fine on Honeycomb and up.
For few users on Gingerbread I have this error too...
Any new idea of the reason?
It can have something in common with Spinner in Action Bar. Do You have any in Your Action Bar?
I don't.
Please add a test case to the known-bugs project on the dev branch.
I've started to see reports of this after a recent update that has Fragments in a ViewPager that provide action items with sub menus. I'll see if I can actually reproduce it myself in an emulator (something which is usually a pain for these sorts of bugs) and extract out a small enough example.
I get similar stack trace when screen orientation changes and there are Fragments in ViewPager and those Fragments contains submenus in ActionBar. I have open question with sample repo on StackOverflow: http://stackoverflow.com/q/13473165/262462
I just met exactly this bug in code. It was caused by inflating Menu in Activity and accessing (searching for MenuItems) it in Fragment. It's probably caused because in some situations (eg. screen rotation) is Fragment re-created faster than Activity and after such event it was accessing Menu not yet inflated.