(不建议使用)片段onOptionsItemSelected没有被调用

问题描述:

这个问题是针对过时的夏洛克动作栏.现在应改为使用Android支持库

This question was for the deprecated sherlock action bar. Android support library should be used instead now

我为我的fragment添加了一个名为共享的操作栏菜单选项,该选项出现了,但未捕获选择事件

I have added an action bar menu option called share for my fragment which appears but the selection event is not being caught

我正在这样添加它

@Override
public void onCreateOptionsMenu (Menu menu, MenuInflater inflater) {
    MenuItem item = menu.add(0, 7,0, R.string.share);
    item.setIcon(R.drawable.social_share).setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
}

试图同时在fragmentfragment activity中捕获它

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()) {
        case 7:
            Intent share = new Intent(Intent.ACTION_SEND);
            share.setType("text/plain");
            share.putExtra(Intent.EXTRA_TEXT, "I'm being sent!!");
            startActivity(Intent.createChooser(share, "Share Text"));
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

,我在onCreate()中有setHasOptionsMenu(true);.

为操作栏夏洛克使用而编辑

Edit for actionbar sherlock use

我不得不使用

public boolean onMenuItemSelected(int featureId, MenuItem item) {

在主要活动中捕获菜单项

in the main activity to capture the menu item