0

As title, I used ActionBarSherlock and SlidingMenu on my APP.

To add menu item on actionbar, what I did is:

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);
    }
  }
}

In most time it runs well, but sometime I'll get this error when I start my 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

And my APP just crashed...

To follow that error message, I did add an empty constructor on MenuFragment, but my APP sometime still force closed by same error.

I also read some thread about this in StackOverflow, but just not understand enough.

What can I do to overcome this problem ?

RRTW
  • 3,160
  • 1
  • 35
  • 54
  • I think it can be because `MenuFragment` is an inner class and it's not static so it needs `Main` class object to be created. You can try to move `MenuFragment` class as an separate class or make it static. – Michał Z. Apr 16 '13 at 07:58
  • I not sure line like : getSupportActionBar(); or public MenuFragment(){} are needed here.this does not solve your problem, but fewer lines of code there is less chance of making an error – letroll Apr 16 '13 at 08:16

1 Answers1

0

OK, I finally fixed this by using this

@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);
}

instead of this

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

And fixed my problem.

RRTW
  • 3,160
  • 1
  • 35
  • 54