无法重写ListFragment中的onCreateOptionsMenu
我创建了一款支持手机和平板电脑版本的应用程序,因此我使用了android-support-v4.jar库。
我的活动扩展了ListFragment,我尝试覆盖onCreateOptionsMenu(Menu Menu,MenuInflater inflater),如以下链接所示:http://developer.android.com/resources/samples/Support4Demos/src/com/example/android/ supportv4 /应用程序/ FragmentMenuSupport.html
我以前叫setHasOptionsMenu。
不幸的是,我似乎无法重写onCreateOptionsMenu()。
这是错误消息:
MyFragment类型的onCreateOptionsMenu(Menu Menu,MenuInflater inflater)方法必须覆盖或实现一个超类型方法。
我做到了这一点:
Public class MyFragment extends ListFragment
确保导入来自兼容性库,而不是来自SDK本身。
  好的,我也遇到了同样的问题,尽管这里并没有解决这个问题。  我使用ActionBarSherlock库,事实证明, onCreateOptionsMenu希望Menu来自android.support.v4.view.Menu和MenuInflater ,来自android.view.MenuInflater ,而不是android.support.v4.view.MenuInflater 。  不要问我为什么。  我不知道这是否能解决所有人的问题,所以我会分享我的想法: 
右键单击您希望方法在Elcipse> Source> Overide / Implement方法中的空白处...
然后从这里找到它,Eclipse会自动导入正确的东西。
在我的活动中使用SherlockActionBar时遇到了类似的问题。 这是我的设置,解决了这个问题:
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;
    }
...
}
上一篇: Unable to override onCreateOptionsMenu in ListFragment
下一篇: How can I refresh the ActionBar when onPrepareOptionsMenu switched menu entries?
