4
votes

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?

11
Do you have a main.xml define in res/menu folder ?Alexis C.
Make sure your import is not import android.R and instead is importing your own generated file properly.brianestey
@AlexisC. I'm having the same issue exactly but I've never been told to make a main.xml file in res/menu from following the 2013 big nerd ranch guide from the beginning. It asks me to create effectively a main.xml but it's in res/layout. What should this res/menu/main.xml file contain?user27886

11 Answers

8
votes

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

3
votes

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.

1
votes

Because you use R.menu.main so your program will find in the res/menu folder for a file main.xml. So all you need is to create a new file main.xml in res/menu. And don't import android.R to your class.

0
votes

Instead of

import android.R;

use

import com.example.R;

where com.example is your own project's namespace.

If you want to find the generated file in your project, it should be under the gen directory. If your project compiles properly, the R file should get generated and placed there automatically. If this doesn't happen, this is when the Project Clean is recommended.

0
votes
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.activity_main, menu);
    return true;
}

Make sure you have the main.xml file in the res\menu folder

Remove "import android.R;"

0
votes

Errors like these could happen because you have an error in the XML file main.xml

Make sure you have no errors in all your resources files and that everything is correct. When there's something wrong there, all references to the res folder from your source code will fail. Also make sure not to import android.R as these are resources from the Android SDK. Just remove that import and it should refer to the resources folder in your project.

0
votes

nothing to do with coding.just install the proper tools and packages using Android sdk manager. If you wanna use android 4.0 (API 14) install all the packages and install packages under Tools. It worked for me.

0
votes

Depending upon how you have run through the setup of your project, generally the folder res/menu may not be autogenerated. In order to inflate a menu you must therefore have a menu to inflate.

Create a new directory within res called 'menu'; then within said directory create an xml file called 'main'. It is this file which the menuInflater will inflate.

0
votes

I had the same Problem then I clicked on the Sync Project with Gradle files and it worked for me You can find that Option in Tools->Android->Sync Project with Gradle files

0
votes

Right click the "res" folder < new < directory.

Name it "menu"

Inside that directory create a new xml called main_menu

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

<item
    android:id="@+id/edit_item"
    android:title="Edit" />
<item
    android:id="@+id/delete_item"
    android:title="Delete" />

-1
votes

The version of Android may not have the folder res / menu. If so, create and copy the xml into the folder to troubleshoot.