Java R.menu 无法解析

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3568182/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 01:55:39  来源:igfitidea点击:

R.menu cannot be resolved

javaandroid

提问by user430945

So I am going through the Android development tutorial and I have run into an early problem when creating menus. I created a folder under rescalled menuand put in game_menu.xmlas I was told.

因此,我正在阅读 Android 开发教程,但在创建菜单时遇到了早期问题。我在res调用下创建了一个文件夹,menugame_menu.xml按照我的指示放入。

But the line inflater.inflate(R.menu.game_menu, menu);is telling me that R.menucannot be resolved. As I probably shouldn't edit R.javaby hand, I would appreciate some help on what to do.

但是线路inflater.inflate(R.menu.game_menu, menu);告诉我R.menu无法解决。由于我可能不应该R.java手动编辑,我希望能得到一些帮助。

采纳答案by Cheryl Simon

Did you build the project after adding game_menu.xml? If so, you should be able to look at the R file and find R.menu.game_menu. You definitly don't want to hand edit it.

加入game_menu.xml 后你建工程了吗?如果是这样,您应该能够查看 R 文件并找到 R.menu.game_menu。您绝对不想手动编辑它。

Did you import R into the java file where you are trying to inflate it?

您是否将 R 导入到要对其进行充气的 java 文件中?

回答by Kevin Coppock

Edit from comments above: Pretty sure your problem is that you haven't imported your graphics into the drawable folder then. Download the PNG files for the tutorial, and drag (or import) them into the res/drawable folder. That should fix your problem. When you reference a drawable with the "@drawable/ic_new_game", you're telling it to look in the res/drawable folder for a resource named ic_new_game (which is probably a PNG image in this case).

从上面的评论中编辑:很确定你的问题是你没有将图形导入到 drawable 文件夹中。下载教程的 PNG 文件,并将它们拖(或导入)到 res/drawable 文件夹中。那应该可以解决您的问题。当您使用“@drawable/ic_new_game”引用可绘制对象时,您是在告诉它在 res/drawable 文件夹中查找名为 ic_new_game 的资源(在本例中可能是 PNG 图像)。

回答by Beer Me

Be sure you're importing your.package.R and not android.R

确保你导入的是 your.package.R 而不是 android.R

That fixed it for me.

那为我修好了。

回答by Koffiman

Be sure to check if the activity has a menu file under res/menu/if you are using XML definitions for the menu.

res/menu/如果您为菜单使用 XML 定义,请务必检查活动下是否有菜单文件。

回答by user3677466

I've had the same problem and here is my solution:

我遇到了同样的问题,这是我的解决方案:

  1. Right click in the res folder in Android-Studio.

  2. Select New->directory.

  3. enter the name "menu" whitout "".

  4. in the tree of res select the folder menu.

  5. right click in folder menu and select New->menu resource file

  6. give a name for your menu and congrats.

  1. 右键单击 Android-Studio 中的 res 文件夹。

  2. 选择新建->目录。

  3. 输入名称“菜单”,而不是“”。

  4. 在 res 树中选择文件夹菜单。

  5. 右键单击文件夹菜单并选择新建->菜单资源文件

  6. 为您的菜单命名并祝贺。

Steps

脚步

回答by Md Ashraf Uzzaman

just simple

很简单

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.game_menu, menu);
    return  true;
}

//done

//完毕

remove this

删除这个

inflater.inflate(R.menu.game_menu, menu) //remove this

inflater.inflate(R.menu.game_menu, menu) //去掉这个