R.menu cannot be resolved
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 probably shouldn't edit R.java
by hand, I would appreciate some help on what to do.
Be sure you're importing your.package.R and not android.R
That fixed it for me.
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?
I've had the same problem and here is my solution:
Right click in the res folder in Android-Studio.
Select New->directory.
enter the name "menu" whitout "".
in the tree of res select the folder menu.
right click in folder menu and select New->menu resource file
give a name for your menu and congrats.
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).
Be sure to check if the activity has a menu file under res/menu/
if you are using XML definitions for the menu.
On Android studio
- make sure you have the right import for R as mentioned above
- rebuild the project by clicking on the build icon
- clean the project by going to Build->Clean Project
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
精彩评论