Unable to override onCreateOptionsMenu in ListFrag

2019-01-31 18:57发布

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 override the onCreateOptionsMenu(Menu menu, MenuInflater inflater), as in the following link: http://developer.android.com/resources/samples/Support4Demos/src/com/example/android/supportv4/app/FragmentMenuSupport.html

I previously called setHasOptionsMenu.

Unfortunately, it seems that I cannot override onCreateOptionsMenu().

This is the error message:

The method onCreateOptionsMenu(Menu menu, MenuInflater inflater) of type MyFragment must override or implements a supertype method.

And I did that with:

Public class MyFragment extends ListFragment

10条回答
老娘就宠你
2楼-- · 2019-01-31 19:30

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);
}
查看更多
老娘就宠你
3楼-- · 2019-01-31 19:31

Make sure the imports are from the compatibility library and not from the SDK itself.

查看更多
对你真心纯属浪费
4楼-- · 2019-01-31 19:38

Try this, actually IDE got confused bw native menu import and Sherlock import..so if we specify it clearly then it will be resolved..

@Override
    public void onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu,
            com.actionbarsherlock.view.MenuInflater inflater) {

}

@Override
    public boolean onOptionsItemSelected(
            com.actionbarsherlock.view.MenuItem item) {
        // TODO Auto-generated method stub

}
查看更多
SAY GOODBYE
5楼-- · 2019-01-31 19:41

i used i used com.actionbarsherlock.view.Menu - maybe it has changed since?

查看更多
登录 后发表回答