【问题标题】:OnCreateOptionsMenu crash below API level 16低于 API 级别 16 的 OnCreateOptionsMenu 崩溃
【发布时间】:2013-08-23 08:56:59
【问题描述】:

我一直在从 Eclipse 中的主/详细流模板构建一个应用程序(我的第一个)。 我已成功将actionbarsherlock 添加到项目中,并且应用程序将运行到API 10,尽管每当我尝试在我的详细信息片段中从onCreateOptionsMenu() 调用mItem.id(所选列表项的ID)时比 API 16 (android 4.1) mItem.id 为空。 API 17 & 18 没问题,好像在action bar前声明了item id。

片段类

public class ModuleDetailFragment extends SherlockFragment implements OnClickListener {
    //The fragment argument representing the item ID that this fragment represents.
    public static final String ARG_ITEM_ID = "item_id";
    // The content this fragment is presenting.
    public ModuleList.ModuleItem mItem;

和 onCreateOptionsMenu:

@Override                                   
public void onCreateOptionsMenu(Menu menu, MenuInflater mi) {
    Toast.makeText(getActivity(), "item id = "+mItem.id, Toast.LENGTH_SHORT).show();
}

我有 if 语句识别按下的按钮并显示正确的操作栏菜单项,但这足以导致崩溃。

任何指针都将不胜感激,谷歌迄今为止返回的很少。

问候,

【问题讨论】:

  • 我可以添加任何其他信息来帮助任何潜在的回答者吗?我错过了什么明显的东西吗?

标签: actionbarsherlock fragment master-detail oncreateoptionsmenu


【解决方案1】:

最终想通了,在使用 getActivity().invalidateOptionsMenu(); 声明 pre Jellybean API 中的变量后,能够查询构建版本并重新初始化菜单栏

可能不是最有效的解决方案,但它对我有用(无论如何蜂窝向上)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-03
    相关资源
    最近更新 更多