android – 从Fragment添加项目到操作栏的选项菜单然后更改方向后崩溃

前端之家收集整理的这篇文章主要介绍了android – 从Fragment添加项目到操作栏的选项菜单然后更改方向后崩溃前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用 ActionBarSherlockViewPagerIndicator将片段显示为选项卡.其中一个片段将项添加到ActionBar:
private String[] mapNames;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);
    // init an String array `mapNames` which is used when populating submenu
    // ...
}
@Override
public void onCreateOptionsMenu(Menu menu,MenuInflater inflater) {

    inflater.inflate(R.menu.fragment_maps,menu);

    SubMenu mapNamesMenu = menu.findItem(R.id.map_names).getSubMenu();
    mapNamesMenu.clear();
    for (int i=0; i<mapNames.length; i++) {
        mapNamesMenu.add(1,i,Menu.NONE,mapNames[i]);
    }

    super.onCreateOptionsMenu(menu,inflater);
}

在res / menu / fragment_maps.xml中我有

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/map_names"
        android:title="Maps"
        android:icon="@drawable/maps_32"
        android:showAsAction="always|withText">
        <menu>
            <item android:id="@+id/placeholder_maps" />
        </menu>
    </item>
</menu>

在旋转手机之前一切正常.方向更改后,此菜单变得不可访问(单击图标时没有任何反应).然后,如果我再次旋转手机,我会收到此错误

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:532)
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:912)
at android.widget.PopupWindow.showAsDropDown(PopupWindow.java:824)
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:273)
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:2494)
at android.view.View$PerformClick.run(View.java:9122)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3806)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)

任何想法如何解决?我使用的是Android 2.3.6

编辑:见test repository

解决方法

我想,这是一个上下文问题.这就是BadTokenException正在发生的原因.

这个例外背后有很多可能性:

1)您可能正在某个地方使用“this”作为上下文引用,它实际上需要YourActivity.this或父活动的上下文.

要么

2)从我猜测的log-cat中,你试图显示一个弹出窗口.

问题可能是,您过早地显示弹出窗口(即在活动生命周期完成之前).
所以等到活动生命周期结束.

要推迟显示弹出窗口,可以参考此link.

总之,这个问题是由于以下用例:

An activity’s reference is passed to the some component (i.e. like
Toast,alert dialog,pop-up etc),and activity destroyed but
that component is still alive or trying to use destroyed activity’s
context.

所以要确保没有这样的情况.

希望你能给你一些解决问题的暗示.

猜你在找的Android相关文章