我在点击菜单选项时没有显示菜单问题。因为我使用了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);
}
}
};
答案 0 :(得分:1)
您应该在片段的onCreate
中致电setHasOptionMenu(true)。
public void setHasOptionsMenu(boolean hasMenu)
报告此片段想要参与填充选项菜单 通过接收对onCreateOptionsMenu的调用(Menu,MenuInflater)和 相关方法。
另外,您应该在onCreateOptionMenu
中创建选项菜单,而不是在onPrepareOptionMenu
中创建选项菜单,如Android文档中所示:
public void onPrepareOptionsMenu(菜单菜单)
准备要显示的屏幕标准选项菜单。这被称为正确 在显示菜单之前,每次显示它。你可以用它 有效地启用/禁用项目或以其他方式动态的方法 修改内容。