开发者

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:

  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.

R.menu cannot be resolved


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

  1. make sure you have the right import for R as mentioned above
  2. rebuild the project by clicking on the build icon
  3. 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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜