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 res called menu and put in game_menu.xml as i was told. But the line inflater.inflate(R.menu.game_menu, menu); is telling me that R.menu cannot be resolved. As i cannot/probably shouldnt edit R.java by hand, i would appreciate some help.
+1
A:
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.
Did you import R into the java file where you are trying to inflate it?
Mayra
2010-08-25 16:43:07
I added game_menu.xml after already creating the project. Right now my R.java file contains onlyhttp://pastebin.com/PKtfkyHB .R works fine for other things such as setContentView(R.layout.main);for main.xml
2010-08-25 16:48:16
So it looks like your R file has not yet picked up your new menu xml file. Did you try saving the xml file, and rebuilding the project? What are you using to build, eclipse? Also, you could try add a resource to one of the other folders, and see if that shows up in R.
Mayra
2010-08-25 17:43:54
Yes i am using Eclipse to build. It seems that the thing it is having a problem with is my adding of a menu folder in the first place. Just recreating a project from scratching and attempting to add a folder called 'menu' under res and adding game_menu.xml causes the project to error. I have tried saving the xml file. I have it set to build automatically when i run the program, should i be doing something differently than than?
2010-08-25 18:13:02
Ok so scratch that first part, i was able to create the folder and file without an error now, but im still back where i started, with not have menu automatically added to R.
2010-08-25 18:20:13
So you are able to add other resources successfully, not just menus? Sometimes having eclipse on build automatically causes problems. I would explicitely tell it to build, then examine your R file.
Mayra
2010-08-25 18:30:11
I have not been able to add any resources successfully. Also turning off automatic building and doing it manually had no effect.
2010-08-25 18:35:25
Ok so it does seem to be that it just is not building correctly when i create the menu folder and insert game_menu.xml. Because when i attempt to add this before building initially, so before the R.java file is even generated in the first place, it fails until i remove this folder.
2010-08-25 19:53:00
What is the error message when it fails to build?
Mayra
2010-08-25 19:57:46
2010-08-25 14:49:15 - HelloAndroid2] W/ResourceType( 2600): Bad XML block: header size 62 or total size 0 is larger than data size 0[2010-08-25 14:49:15 - HelloAndroid2] C:\svnwork\HelloAndroid2\res\menu\game_menu.xml:3: error: Error: No resource found that matches the given name (at 'icon' with value '@drawable/ic_new_game'). Few more errors that are basically the same but with different resource matches not found.
2010-08-25 19:59:30
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).
kcoppock
2010-08-25 20:09:21
That fixed it, along with adding New Game and Quit to the Strings list. Thanks.
2010-08-25 20:27:59
Glad to help! Eclipse is pretty tricky to get the hang of (especially if you're coming from Visual Studio...) in my opinion.
kcoppock
2010-08-25 20:37:32
+1
A:
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).
kcoppock
2010-08-25 17:39:46