Container中按确认键动态弹出菜单的一步用法是
TKeyResponse CContextMenuContainer::OfferKeyEventL(const TKeyEvent& aKeyEvent,TEventCode aType) { // If the Select Key (OK key in emulator) is pressed, show the context sensitive menu TBool selectKeyPressed = (aType == EEventKey) && (aKeyEvent.iCode == EKeyOK); TBool savedGameListNotEmpty = (iSavedGamesListBox) && (iSavedGamesListBox->Model()->NumberOfItems() > 0);
if (selectKeyPressed && savedGameListNotEmpty) { // get the menu bar from the parent of this control. CEikMenuBar* parentMenuBar = iEikonEnv->AppUiFactory()->MenuBar();
// Set the menu bar to the context-sensitive menu bar parentMenuBar->SetMenuTitleResourceId(R_CONTEXTMENU_SAVED_GAMES_MENU_BAR);
// try to display the context sensitive menu bar if (parentMenuBar) { parentMenuBar->TryDisplayMenuBarL(); }
// change the menu bars resource to the normal menu bar so that the left softkey displays // the correct menu
parentMenuBar->SetMenuTitleResourceId(R_CONTEXTMENU_MENU_BAR);
return EKeyWasConsumed; }
// Otherwise, offer the key to the listbox to process. else return iSavedGamesListBox->OfferKeyEventL (aKeyEvent, aType); }
CEikMenuBar* parentMenuBar = iEikonEnv->AppUiFactory()->MenuBar(); 一开始在container模式下没有问题。换成View+container模式就出错,说parentMenuBar 是空指针,解决办法如下,将View指针传递到Container中,将上边语句换成
CEikMenuBar* parentMenuBar =iParentView-> MenuBar();
就可解决。