JakeWharton / ActionBarSherlock

[DEPRECATED] Action bar implementation which uses the native action bar on Android 4.0+ and a custom implementation on pre-4.0 through a single API and theme.
http://actionbarsherlock.com
Apache License 2.0
7.1k stars 3.54k forks source link

WindowManager crash - unable to add window #641

Open petrnohejl opened 11 years ago

petrnohejl commented 11 years ago

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.

iNoles commented 11 years ago

android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?

Did you declare it in AndroidMainfest.xml?

petrnohejl commented 11 years ago

Yes, I did. On Jelly Bean, it works fine.

krisread commented 11 years ago

Get this on Android 2.3 works fine on Honeycomb and up.

pommedeterresautee commented 11 years ago

For few users on Gingerbread I have this error too...

Any new idea of the reason?

kkocel commented 11 years ago

It can have something in common with Spinner in Action Bar. Do You have any in Your Action Bar?

petrnohejl commented 11 years ago

I don't.

SimonVT commented 11 years ago

Please add a test case to the known-bugs project on the dev branch.

willlunniss commented 11 years ago

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.

Kuitsi commented 11 years ago

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

c4t-dr34m commented 11 years ago

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.