Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix "R.menu.main" in java when first developing an android app?

So I downloaded everything that the Android app tutorial told me to, and I am and using Eclipse, but I keep getting an error message on main in R.menu.main:

public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;

I've imported android.R; and it didn't work; I deleted that line and I went to Project>Clean and it still didn't fix itself. Other people on this topic keep saying to import the "R file" from your package but I checked all of the packages and classes and there is nothing called R. I tried importing the res folder and that didn't work either. Help?

like image 613
user2502058 Avatar asked Dec 21 '22 04:12

user2502058


2 Answers

You should have a main.xml file in your res folder to point to. If you have that and it's still not working then it might be a problem with the R file. That has been known to cause issues. You shouldn't need to import R since it builds automatically.

Chances are there is an error in your xml file. This will cause the R file to not build. Fix that issue and rebuild your project and you should be fine

like image 102
Ryan Sayles Avatar answered Jan 13 '23 14:01

Ryan Sayles


To what Android app tutorial are you referring?

I'm going to assume that you're so new at this that you have a thousand questions no matter where you turn. The trick for me, at that stage, was to just get something, anything, working. After that, I'd just keep building, carefully, inside of that first success (well, a copy of it, actually... I was that paranoid).

So... I'm offering here to quickly take you through the steps in creating an app in your Android/Eclipse environment. If things start breaking down then perhaps your setup isn't right. But if your setup is good then you should have a running app in about ten minutes.

From that, you'll see that you don't have to worry about a lot of the details in the answers given for your Stackoverflow [this] post (at least, not until you gain more experience).

(note that this stuff, in greater detail, is found here but I'm going to give you a seriously "let's just get this going already" version)

In the Eclipse menus...

File -> New -> Android Application Project

a "New Android Application" dialog window will come up

Type "MyTestApp" in the Application Name field and simply accept everything else. Click Next.

You'll be getting four more dialog windows after that first one. Accept all of the defaults on each of them and simply keep clicking Next until you get to the last dialog. There, just click Finish.

At this point, the project is being created. Depending on your computer, this might take a few seconds. You can see progress with the messaging displayed in the bottom right corner of the Eclipse window. See nothing there... it's created.

If your Android/Eclipse setup is good then you should soon see a "MyTestApp" project in the Package Explorer. The project will be open (you'll see a bunch of folders in a hierarchy). "activity_main.xml" will be highlighted under the "layout" folder, all of this under the MyTestApp project folder.

Click the MyTestApp project folder, to select it. Then, in the Eclipse menus...

Project -> Build Project

Again, watch the status messaging at the bottom right of the Eclipse window. If your computer is quick then you may not look in time to catch them. See nothing there... your project is built.

In the Eclipse menus...

Run -> Run

Select Android Application in the dialog and click OK.

At this point, you may or may not get a device dialog. If you do, choose to Launch a new Android Virtual Device (an emulator). You may need to go to the Manager (there's a button for this), to set up a device. I use AVD_for_Nexus_One_by_Google. I know that one works.

Be forewarned that the emulator will seem to take forever to start up. Be patient. Be VERY patient. You should see the app running shortly.

So, assuming you see a running application in the emulator... let's see what you ended up not having to worry about...

If you examine the auto-generated MainActivity.java file, in the project that you just created, you'll see that the code that you were concerned about...

getMenuInflater().inflate(R.menu.main, menu);

... seems fine. No error indications.

You'll also notice that there is no "R-type" import at the top of the source code.

Notice, if you dig down to the bottom of the "gen" folder, under your "MyTestApp" project folder, in the Package Explorer, that there's an R.java file. This is automatically generated when the project is built and is the file to which your getMenuInflater refers.

The point to all of this is that you'll have (if your Android/Eclipse setup is good) something working, giving you a launch point to play with other things.

Good luck. I've sure been having a blast.

like image 44
UpLate Avatar answered Jan 13 '23 15:01

UpLate