在创建菜单c #android时,当前上下文中不存在 [英] does not exists in the current context while creating menu c# android

查看:49
本文介绍了在创建菜单c #android时,当前上下文中不存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在android api 10(2.3.5)c#中创建了一个应用程序。

我使用xml文件MainMenu作为菜单。我使用此代码创建菜单:



public bool onCreateOptionsMenu(菜单菜单)

{

MenuInflater inflater = getMenuInflater();

inflater.inflate(Resource.Layout.MainMenu,menu);

返回true;

}





但我收到错误getMenuInflater在当前情况下不存在



请告诉我有什么问题。

I create an app in android api 10(2.3.5) c#.
I use xml file MainMenu for menu. I use this code for creating menu:

public bool onCreateOptionsMenu(Menu menu)
{
MenuInflater inflater = getMenuInflater();
inflater.inflate(Resource.Layout.MainMenu, menu);
return true;
}


But I get error getMenuInflater does not exists in the current context

please tell me what is the problem.

推荐答案

尝试将此行更改为

Try changing this line to
MenuInflater inflater = getMenuInflater();



to


to

MenuInflater inflater = getActivity().getMenuInflater();



i希望你在


i hope you have created your menu inside

<menu></menu>

标签内正确创建菜单。如果没有,请参阅本教程菜单

tags correctly. if not Refer this tutorial Menus


public override bool OnPrepareOptionsMenu

(IMenu菜单)

{

this.MenuInflater.Inflate(Resource.Layout.MainMenu,menu);

返回base.OnPrepareOptionsMenu(菜单);

}
public override bool OnPrepareOptionsMenu
(IMenu menu)
{
this.MenuInflater.Inflate (Resource.Layout.MainMenu, menu);
return base.OnPrepareOptionsMenu (menu);
}


这篇关于在创建菜单c #android时,当前上下文中不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆