我在 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;
}
});
}