在Sherlock FragmentActivity中添加菜单栏

时间:2014-01-20 08:22:06

标签: android android-intent

我想在我的课程中添加菜单获取错误* 无法覆盖SherlockFragment * 的最终方法请告诉我该怎么做

我的代码是

public class FragementFirst extends SherlockFragment{

    Button btn;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

                View rootView = inflater.inflate(R.layout.fragementone, container, false);
                btn = (Button) rootView.findViewById(R.id.butto);
                setHasOptionsMenu(true);
                btn.setOnClickListener(new  OnClickListener() {
            @Override
            public void onClick(View v) {
             Intent intent = new Intent(getActivity(), FirstActiviry.class);
              getActivity().startActivity(intent);

            }
        });
        return rootView;
    }
    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
        inflater.inflate(R.menu.first, menu);
    }
}

    <menu xmlns:android="http://schemas.android.com/apk/res/android" >

<item android:id="@+id/action_task"
      android:icon="@drawable/icon_xhdpi"
      android:title="hello"
      android:showAsAction="ifRoom" />
</menu>

1 个答案:

答案 0 :(得分:0)

请使用

@Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
        getSupportMenuInflater().inflate(R.menu.first, menu);
    } 

而不是

@Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
        inflater.inflate(R.menu.first, menu);
    }

理解教程是有的 请访问http://www.grokkingandroid.com/adding-action-items-from-within-fragments/