简体   繁体   中英

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?

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

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.

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.

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;"

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.

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.

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.

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.

我遇到了同样的问题,然后我点击了 Sync Project with Gradle files,它对我有用你可以在 Tools->Android->Sync Project with Gradle files 中找到该选项

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" />

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM