OptionMenu在Sherlock片段中没有显示任何内容

时间:2013-07-05 08:02:18

标签: android android-fragments actionbarsherlock optionmenu

我在Sherlock Fragment工作,需要在optionMenu中添加一些选项。但无法做到这一点。这是我的代码如下。我已经在onCreateView中添加了setHasOptionMenu(true):所以请看一下这段代码,,,告诉我哪里错了

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

        progress = menu.add("Progress");// first option
        progress.setIcon(android.R.id.progress);
        progress.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);

        MenuItem add = menu.add("Refresh");// second option
        add.setIcon(R.drawable.ic_menu_refresh);
        add.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);

        MenuItem get = menu.add("Logout");// third option
        get.setIcon(R.drawable.power);
        get.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);



        get.setOnMenuItemClickListener(new OnMenuItemClickListener() 
        {

            @Override
            public boolean onMenuItemClick(MenuItem item)
            {

                userfunctions = new UserFunctions();
                       //AsyncTask
                class AsyncLogout extends AsyncTask<Void, Void, String> {

                    protected String doInBackground(Void... params) {
                        json = userfunctions.logoutUser(userid);



                        try {
                            if (json.getString(KEY_SUCCESS) != "1") {
                                String res = json.getString(KEY_SUCCESS);

                                if (Integer.parseInt(res) == 1) {
                                    p_error_msg = "successful";

                                }

                            } else {
                                p_error_msg = "error";

                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                        return p_error_msg;
                    }

                    protected void onPostExecute(String result) {
                        try {
                            if (result.equals("successful")) {

                                Intent ii = new Intent(getActivity(),
                                        HomeActivity.class);

                                SharedPreferences.Editor editor = mPrefs.edit();

                                editor.putString("login", "0");

                                editor.remove("PREF_ACCESS_TOKEN_TWITTER");
                                editor.remove("PREF_ACCESS_TOKEN_SECRET_TWITTER");
                                editor.remove("PREF_ACCESS_TOKEN_FB");
                                editor.remove("PREF_ACCESS_TOKEN_SECRET_FB");

                                editor.commit();

                                startActivity(ii);
                                getActivity().finish();
                            } else {

                            }

                        } catch (Exception e) {

                        }

                    }

                    protected void onPreExecute() {
                        super.onPreExecute();

                    }
                }

                AsyncLogout as=new AsyncLogout();
                as.execute();

                return false;
            }
        });

        add.setOnMenuItemClickListener(new OnMenuItemClickListener() {

            @Override
            public boolean onMenuItemClick(MenuItem item) 
            {
                Intent i = new Intent(getActivity(), MainActivity.class);
                SharedPreferences.Editor editor = mPrefs.edit();
                editor.putString("login", "1");
                editor.putInt("defaultSelector", 2);
                editor.commit();
                startActivity(i);
                getActivity().finish();

                return false;
            }
        });



 }

1 个答案:

答案 0 :(得分:1)

以下是从MenuItems处理Fragment的正确方法:

    public class MyFragment extends Fragment {

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

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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        super.onOptionsItemSelected(item);
        switch(item.getItemId()){
        case R.id.action_sort:
                // DO STUFF 
            break;
        case R.id.action_anytag:
                // DO STUFF
            break;
        }
        return true;
    }
}

我的建议是,不是在Java代码中创建项目,而是仅为menu.xml使用自定义Fragment,对其进行充气并使用onOptionsItemSelected()来处理菜单项上的点击事件。