Unable to override onCreateOptionsMenu in ListFragment

后端 未结 10 1664
太阳男子
太阳男子 2020-12-24 06:56

I created an app that supports both phone and tablet version so i use the android-support-v4.jar library.

My activity extends the ListFragment and I tried to overri

相关标签:
10条回答
  • 2020-12-24 06:59

    Had the same problem, but it was because I used the wrong onCreateOptionsMenu method in my Fragment!

    boolean onCreateOptionsMenu(Menu menu) is only for Activities.

    @Override //For Activities
    public boolean onCreateOptionsMenu(Menu menu) { 
    ...
    

    Had to move it to the activity class containing the Fragment.

    Fragment have their own: void onCreateOptionsMenu (Menu menu, MenuInflater inflater)

    @Override //For Fragments.
    public void onCreateOptionsMenu (Menu menu, MenuInflater inflater){
    ...
    

    Creating an Options Menu: http://developer.android.com/guide/topics/ui/menus.html

    0 讨论(0)
  • 2020-12-24 07:03

    I had a similar issue using the SherlockActionBar on my activity. Here was my setup that fixed the problem:

    import com.actionbarsherlock.app.SherlockActivity;
    import com.actionbarsherlock.view.Menu;
    
    public class LoginActivity extends SherlockActivity{
    
        ...
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu){
            getSupportMenuInflater().inflate(R.menu.activity_login, menu);
            return true;
        }
    
        ...
    
    }
    
    0 讨论(0)
  • 2020-12-24 07:08

    Ouch!!! That was a good one!

    I imported android.view.Menu in MyFragment instead of android.support.v4.Menu!

    I lost a few hours on this one! Hope this post can at least help someone else.

    0 讨论(0)
  • 2020-12-24 07:12

    Just had the same issue in an activity on Xamarin. it was expecting the method to take Xamarin.ActionbarSherlockBinding.Views.IMenu as an argument.

    How to find out: -Comment the OnCreateOptionsMenu method You started to implement. -In some working method start typing OnCreateOptionsMenu like You want to call it. -Choose it from the suggestions list. -Place a cursor on OnCreateOptionsMenu call. -press Command+d to go to assembly browser. You will see the interface from implementation. -Then by pressing mouse pointer on the parameter type it takes You will get to the interface of this type implementation. -And You will see namespace it is in.

    0 讨论(0)
  • 2020-12-24 07:12

    I had the same problem and this what I did to use onCreateOptionsMenu of Fragment. Override the onCreate method of the Fragment and make sure that you use setHasOptionsMenu method with parameter value "true" to let the system know Fragment will use OptionsMenu.

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

    Then override onCreateOptionsMenu to inflate your menu xml file (here in this example I inflated fragmentmenu.xml

    @Override
    public void onCreateOptionsMenu (Menu menu, MenuInflater inflater) {
         inflater.inflate(R.menu.fragmentmenu, menu);
    }
    
    0 讨论(0)
  • 2020-12-24 07:16

    OK, I just had this same problem, although it wasn't fixed by what is here. I'm using the ActionBarSherlock library and it turns out that onCreateOptionsMenu wants Menu to be from android.support.v4.view.Menu and MenuInflater to be from android.view.MenuInflater, not android.support.v4.view.MenuInflater. Don't ask me why. I don't know if this will fix everyone, so I'll share how I figured it out:

    Right click the blank space where you'd like the method to be in Elcipse > Source > Overide/Implement methods...

    Then just find it from here, and Eclipse will automatically import the correct things.

    0 讨论(0)
提交回复
热议问题