为什么 onPrepareOptionsMenu(Menu menu) 和 onOptionsItemSelected(MenuItem item) 不显示任何菜单?
Why are onPrepareOptionsMenu(Menu menu) and onOptionsItemSelected(MenuItem item) not displaying any menu?
我有一个关于菜单的问题,当我点击菜单选项时没有显示。由于我使用了onPrepareOptionsMenu(),我期望我已经动态修改了菜单内容,但是即使在我应用了onOptionsItemSelected() 方法之后它也没有显示任何菜单。
*下面的代码实际上是从库中检索到的,用于我的 android 项目。我的项目包含片段,而库中的代码以 Activity.
的形式
代码如下:
PedometerFragment.java
private static final int MENU_SETTINGS = 8;
private static final int MENU_QUIT = 9;
private static final int MENU_PAUSE = 1;
private static final int MENU_RESUME = 2;
private static final int MENU_RESET = 3;
/* Creates the menu items */
public void onPrepareOptionsMenu(Menu menu) {
menu.clear();
if (mIsRunning) {
menu.add(0, MENU_PAUSE, 0, R.string.pause)
.setIcon(android.R.drawable.ic_media_pause)
.setShortcut('1', 'p');
}
else {
menu.add(0, MENU_RESUME, 0, R.string.resume)
.setIcon(android.R.drawable.ic_media_play)
.setShortcut('1', 'p');
}
menu.add(0, MENU_RESET, 0, R.string.reset)
.setIcon(android.R.drawable.ic_menu_close_clear_cancel)
.setShortcut('2', 'r');
menu.add(0, MENU_SETTINGS, 0, R.string.settings)
.setIcon(android.R.drawable.ic_menu_preferences)
.setShortcut('8', 's')
.setIntent(new Intent(getActivity(), Settings.class));
menu.add(0, MENU_QUIT, 0, R.string.quit)
.setIcon(android.R.drawable.ic_lock_power_off)
.setShortcut('9', 'q');
}
/* Handles item selections */
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case MENU_PAUSE:
unbindStepService();
stopStepService();
return true;
case MENU_RESUME:
startStepService();
bindStepService();
return true;
case MENU_RESET:
resetValues(true);
return true;
case MENU_QUIT:
resetValues(false);
unbindStepService();
stopStepService();
mQuitting = true;
getActivity().finish();
return true;
}
return false;
}
// TODO: unite all into 1 type of message
private StepService.ICallback mCallback = new StepService.ICallback() {
public void stepsChanged(int value) {
mHandler.sendMessage(mHandler.obtainMessage(STEPS_MSG, value, 0));
}
public void paceChanged(int value) {
mHandler.sendMessage(mHandler.obtainMessage(PACE_MSG, value, 0));
}
public void distanceChanged(float value) {
mHandler.sendMessage(mHandler.obtainMessage(DISTANCE_MSG,
(int) (value * 1000), 0));
}
public void speedChanged(float value) {
mHandler.sendMessage(mHandler.obtainMessage(SPEED_MSG,
(int) (value * 1000), 0));
}
public void caloriesChanged(float value) {
mHandler.sendMessage(mHandler.obtainMessage(CALORIES_MSG,
(int) (value), 0));
}
};
private static final int STEPS_MSG = 1;
private static final int PACE_MSG = 2;
private static final int DISTANCE_MSG = 3;
private static final int SPEED_MSG = 4;
private static final int CALORIES_MSG = 5;
private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case STEPS_MSG:
mStepValue = (int) msg.arg1;
mStepValueView.setText("" + mStepValue);
break;
case PACE_MSG:
mPaceValue = msg.arg1;
if (mPaceValue <= 0) {
mPaceValueView.setText("0");
} else {
mPaceValueView.setText("" + (int) mPaceValue);
}
break;
case DISTANCE_MSG:
mDistanceValue = ((int) msg.arg1) / 1000f;
if (mDistanceValue <= 0) {
mDistanceValueView.setText("0");
} else {
mDistanceValueView
.setText(("" + (mDistanceValue + 0.000001f))
.substring(0, 5));
}
break;
case SPEED_MSG:
mSpeedValue = ((int) msg.arg1) / 1000f;
if (mSpeedValue <= 0) {
mSpeedValueView.setText("0");
} else {
mSpeedValueView.setText(("" + (mSpeedValue + 0.000001f))
.substring(0, 4));
}
break;
case CALORIES_MSG:
mCaloriesValue = msg.arg1;
if (mCaloriesValue <= 0) {
mCaloriesValueView.setText("0");
} else {
mCaloriesValueView.setText("" + (int) mCaloriesValue);
}
break;
default:
super.handleMessage(msg);
}
}
};
您应该在片段的 onCreate
中调用 setHasOptionMenu(true)。
public void setHasOptionsMenu (boolean hasMenu)
Report that this fragment would like to participate in populating the options menu
by receiving a call to onCreateOptionsMenu(Menu, MenuInflater) and
related methods.
此外,您应该在 onCreateOptionMenu
中创建选项菜单,而不是在 onPrepareOptionMenu
中,正如您在 Android 文档中看到的那样:
public void onPrepareOptionsMenu (Menu menu)
Prepare the Screen's standard options menu to be displayed. This is called right
before the menu is shown, every time it is shown. You can use this
method to efficiently enable/disable items or otherwise dynamically
modify the contents.
我有一个关于菜单的问题,当我点击菜单选项时没有显示。由于我使用了onPrepareOptionsMenu(),我期望我已经动态修改了菜单内容,但是即使在我应用了onOptionsItemSelected() 方法之后它也没有显示任何菜单。 *下面的代码实际上是从库中检索到的,用于我的 android 项目。我的项目包含片段,而库中的代码以 Activity.
的形式代码如下:
PedometerFragment.java
private static final int MENU_SETTINGS = 8;
private static final int MENU_QUIT = 9;
private static final int MENU_PAUSE = 1;
private static final int MENU_RESUME = 2;
private static final int MENU_RESET = 3;
/* Creates the menu items */
public void onPrepareOptionsMenu(Menu menu) {
menu.clear();
if (mIsRunning) {
menu.add(0, MENU_PAUSE, 0, R.string.pause)
.setIcon(android.R.drawable.ic_media_pause)
.setShortcut('1', 'p');
}
else {
menu.add(0, MENU_RESUME, 0, R.string.resume)
.setIcon(android.R.drawable.ic_media_play)
.setShortcut('1', 'p');
}
menu.add(0, MENU_RESET, 0, R.string.reset)
.setIcon(android.R.drawable.ic_menu_close_clear_cancel)
.setShortcut('2', 'r');
menu.add(0, MENU_SETTINGS, 0, R.string.settings)
.setIcon(android.R.drawable.ic_menu_preferences)
.setShortcut('8', 's')
.setIntent(new Intent(getActivity(), Settings.class));
menu.add(0, MENU_QUIT, 0, R.string.quit)
.setIcon(android.R.drawable.ic_lock_power_off)
.setShortcut('9', 'q');
}
/* Handles item selections */
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case MENU_PAUSE:
unbindStepService();
stopStepService();
return true;
case MENU_RESUME:
startStepService();
bindStepService();
return true;
case MENU_RESET:
resetValues(true);
return true;
case MENU_QUIT:
resetValues(false);
unbindStepService();
stopStepService();
mQuitting = true;
getActivity().finish();
return true;
}
return false;
}
// TODO: unite all into 1 type of message
private StepService.ICallback mCallback = new StepService.ICallback() {
public void stepsChanged(int value) {
mHandler.sendMessage(mHandler.obtainMessage(STEPS_MSG, value, 0));
}
public void paceChanged(int value) {
mHandler.sendMessage(mHandler.obtainMessage(PACE_MSG, value, 0));
}
public void distanceChanged(float value) {
mHandler.sendMessage(mHandler.obtainMessage(DISTANCE_MSG,
(int) (value * 1000), 0));
}
public void speedChanged(float value) {
mHandler.sendMessage(mHandler.obtainMessage(SPEED_MSG,
(int) (value * 1000), 0));
}
public void caloriesChanged(float value) {
mHandler.sendMessage(mHandler.obtainMessage(CALORIES_MSG,
(int) (value), 0));
}
};
private static final int STEPS_MSG = 1;
private static final int PACE_MSG = 2;
private static final int DISTANCE_MSG = 3;
private static final int SPEED_MSG = 4;
private static final int CALORIES_MSG = 5;
private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case STEPS_MSG:
mStepValue = (int) msg.arg1;
mStepValueView.setText("" + mStepValue);
break;
case PACE_MSG:
mPaceValue = msg.arg1;
if (mPaceValue <= 0) {
mPaceValueView.setText("0");
} else {
mPaceValueView.setText("" + (int) mPaceValue);
}
break;
case DISTANCE_MSG:
mDistanceValue = ((int) msg.arg1) / 1000f;
if (mDistanceValue <= 0) {
mDistanceValueView.setText("0");
} else {
mDistanceValueView
.setText(("" + (mDistanceValue + 0.000001f))
.substring(0, 5));
}
break;
case SPEED_MSG:
mSpeedValue = ((int) msg.arg1) / 1000f;
if (mSpeedValue <= 0) {
mSpeedValueView.setText("0");
} else {
mSpeedValueView.setText(("" + (mSpeedValue + 0.000001f))
.substring(0, 4));
}
break;
case CALORIES_MSG:
mCaloriesValue = msg.arg1;
if (mCaloriesValue <= 0) {
mCaloriesValueView.setText("0");
} else {
mCaloriesValueView.setText("" + (int) mCaloriesValue);
}
break;
default:
super.handleMessage(msg);
}
}
};
您应该在片段的 onCreate
中调用 setHasOptionMenu(true)。
public void setHasOptionsMenu (boolean hasMenu)
Report that this fragment would like to participate in populating the options menu by receiving a call to onCreateOptionsMenu(Menu, MenuInflater) and related methods.
此外,您应该在 onCreateOptionMenu
中创建选项菜单,而不是在 onPrepareOptionMenu
中,正如您在 Android 文档中看到的那样:
public void onPrepareOptionsMenu (Menu menu)
Prepare the Screen's standard options menu to be displayed. This is called right before the menu is shown, every time it is shown. You can use this method to efficiently enable/disable items or otherwise dynamically modify the contents.