Android ActionBarSherlock和SlidingMenu,菜单片段错误

时间:2013-04-16 07:48:42

标签: android actionbarsherlock fragment slidingmenu

作为标题,我在我的APP上使用了ActionBarSherlock和SlidingMenu。

要在操作栏上添加菜单项,我所做的是:

public class Main extends SherlockFragmentActivity
{
  protected void onCreate(Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);
    setTheme(R.style.Theme_Sherlock);
    requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
    getSupportActionBar();
    setContentView(R.layout.main);

    FragmentManager fm=getSupportFragmentManager();
    FragmentTransaction ft=fm.beginTransaction();
    menuFrag=fm.findFragmentByTag("f1");
    if(menuFrag==null)
    {
      menuFrag=new MenuFragment();
      ft.add(menuFrag, "f1");
    }
    ft.commit();

    //...other stuff
  }

  /**
  * A fragment that displays a menu.  This fragment happens to not
  * have a UI (it does not implement onCreateView), but it could also
  * have one if it wanted.
  */
  @SuppressLint("ValidFragment")
  public class MenuFragment extends SherlockFragment
  {
    public MenuFragment(){}

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
      super.onCreate(savedInstanceState);
      setHasOptionsMenu(true);
    }

    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
    {
      itemProgram=menu.add(0, MENU_ID_PROGRAMS, 0, getString(R.string.menuProgram));
      itemProgram.setIcon(R.drawable.icon_programs_select).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);

      itemMyList=menu.add(0, MENU_ID_MYLIST, 0, getString(R.string.menuMyList));
      itemMyList.setIcon(R.drawable.icon_mylist).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);

      itemPlaying=menu.add(0, MENU_ID_PLAYING, 0, getString(R.string.menuPlaying));
      itemPlaying.setIcon(R.drawable.icon_playing).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
    }
  }
}

在大多数情况下它运行良好,但有时我在启动APP时会出现此错误

android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment 
make sure class name exists, is public, and has an empty constructor that is public

我的APP刚崩溃......

要关注该错误消息,我确实在MenuFragment上添加了一个空构造函数,但我的APP有时仍会因同样的错误而被强制关闭。

我还在StackOverflow中阅读了一些有关此内容的帖子,但还不够了解。

我该怎么做才能解决这个问题?

1 个答案:

答案 0 :(得分:0)

好的,我最后通过使用

解决了这个问题
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
  itemProgram=menu.add(0, MENU_ID_PROGRAMS, 0, getString(R.string.menuProgram));
  itemProgram.setIcon(R.drawable.icon_programs_select).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);

  itemMyList=menu.add(0, MENU_ID_MYLIST, 0, getString(R.string.menuMyList));
  itemMyList.setIcon(R.drawable.icon_mylist).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);

  itemPlaying=menu.add(0, MENU_ID_PLAYING, 0, getString(R.string.menuPlaying));
  itemPlaying.setIcon(R.drawable.icon_playing).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
  return super.onCreateOptionsMenu(menu);
}

而不是

public class MenuFragment extends SherlockFragment
{
  public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
  {
    //Some stuff...
  }
}

解决了我的问题。