views:

23449

answers:

25

Hi, I just downloaded and installed the new android sdk. I wanted to create a simple application to test drive it.

the widzard creates this code:

package eu.mauriziopz.gps;

import android.app.Activity;
import android.os.Bundle;

public class ggps extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}

but ecplise give me this error

R cannot be resolved

on this line

setContentView(R.layout.main);

can any one explain me why?

ps. I do have an xml file named main.xml under res/layout/

A: 

R is a generated class. If you are using the Android Development Tools (ADT) it is generated whenever the project is built. Maybe you have 'Build Automatically' turned off?

Josef
+4  A: 

R is an automatically generated class that holds the constants used to identify your resources. If you don't have an R.java file (it would be gen/eu.mauriziopz.gps/R.java in Eclipse with the 1.5 SDK) I would recommend closing and reopening your project or going to Project > Build all (and selecting "Build Automatically" while there as recommended by Josef). If that doesn't work than try making a new project, if the problem is recreated than post here again and we'll go into more detail.

Will
+2  A: 

What Will said was right

R is an automatically generated class that holds the constants used to identify your >resources. If you don't have an R.java file (it would be gen/eu.mauriziopz.gps/R.java in >Eclipse with the 1.5 SDK) I would recommend closing and reopening your project or going to >Project > Build all (and selecting "Build Automatically" while there as recommended by >Josef). If that doesn't work than try making a new project, if the problem is recreated than >post here again and we'll go into more detail.

but I've found out that there was another problem that was causing the first one. The tools in the SDK directory didn't have the permission to be executed, so it was like the didn't exist for eclipse, thus it didn't build the R.java file

So modifying the permission and selectiong "Build Automatically" solved the problem.

Thanks to all

MaurizioPz
A: 

my project have include a r.java.at the beginning ,R.layout.main work good.But,after adding some code it doesn't work,and the error is R.layout.main can't resolved.what's the problem?

should be a comment
Casebash
+15  A: 

> my project have include a r.java.at the beginning ,R.layout.main work good.But,after adding some code it doesn't work,and the error is R.layout.main can't resolved.what's the problem? >

Look at your imports. Chances are that the line:

import android.R;

will be there. If that's the case, remove it, so that your project will resolve R not with the default Android Resources class, but with the one auto-generated from your /res/ folder.

Dimitar Dimitrov
Thanks @Dimitar,that fixed my issue! Eclipse and the auto-import feature seem to have gotten me by adding that import line automatically.
shambleh
I've also had similar issues when I named a layout using capital letters
Travis
A: 

I had this problem as well, it turned out that I had inadvertently deleted the "app_name" string resource from the strings.xml file, which was causing a silent error. Once I added it back, the R class was generated successfully and everything was back up and running.

HTH.

Guy Starbuck
A: 

I meet the same erro as you.I want to know how modifying the permission ??thx

Should be a comment
Casebash
user clearly doesn't have enough rep for comments so cut some slack.
stealthcopter
A: 

In case anyone is interested (I might be saving your life here) I had the error - R.xml cannot be resolved - slightly different - on a GLS project. hmmmm. After looking in R.java I found an auto generated class XML.java (I think) was not there.

Solution? It needed a new folder in res: res\xml and a file called default_values.xml in there. Then all OK.

Just in case you have not got that file its:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
    xmlns:android="http://schemas.android.com/apk/res/android"&gt;
</PreferenceScreen>
phil
+5  A: 

Hello, Each time I had a problem with R not been generated, or even disapeared, this was due to some problem in xml layout file that prevent the application from beeing built. Hope this can help.

Luc
+1  A: 

R.java is a file that the android eclipse plugins creates while bulding your application. R.java is created under the "gen" directory. This file is generated from the information in the "res" directory. If you run select "Project" -> "Clean..." on the eclipse menu it will remove and then regenerate the R.java file.

The problem "R cannot be resolved" happens when you change your package name in the AndroidManifest.xml file. It uses your android package name to create a subdirectory under the "gen" directory where it stores the R.java file.

Eclipse may have problems executing clean because it is confused about where the R.java file is when you have changed the android package name, you can either rename the subdirectory under gen to match your new package name, or you can change your package name back to the old name, do the clean and then change the package name to the new name you want. This works best if you stop eclipse from trying to build while you are changing the package name. Under the "Project" menu uncheck the option to "Build Automatically" and also when the "Clean..." dialog asks if it should "Start a build immediately" uncheck the box so it doesn't try to build while you are changing the package name. After you have changed the name you can turn "Build Automatically" back on again.

Note that if your AndroidManifest.xml file package name does not match your java package name eclipse will end up automatically adding an "import .R;" line in all your .java files that have any references to R. If you change your AndroidManifest.xml package name, sometimes eclipse does not update all of these added imports. If that happens use the eclipse refactoring (Keys ALT + SHIFT + R) to change the import statement in one of your java files to your new AndroidManifest.xml package name. It is best to do this while you have disabled "Build Automatically"

-- You received this message because you are subscribed to the Google Groups "Android Beginners" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/android-beginners?hl=en

rajinikanth
This looks like a cut and paste without reference
Casebash
A: 

I changed android:versionName in AndroidManifest.xml from 1.0 to 1.1. That regenerated my R.java and I'm a happy camper. Hope this might help somebody

StrefanA
A: 

Check the xml file names. Be sure that they're all in lowercase.

TonyS
A: 

Yet another reason R.java might not get autogenerated is if you have directories like res/drawable-hdpi, res/drawable-mdpi, or res/drawable-ldpi. 1.6+ seems to be OK w/ these directories, but 1.5 doesn't want them. When I removed those directories, R.java started autogenerating for me again.

beppu
A: 

try make your new xml layout file be lower case file name, for example, use my_file.xml instead of myFile.xml

Malcom
+4  A: 

Whenever you got

R cannot be resolved

then check for the /res directory and there must be some file that have some error in it and that is preventing the application from being built. For example It may be layout file or It may be due to some resource is missing but you already defined it in xml file.

Hope this can help.

Kaillash
I had this problem with the sample ApiDemos. Some missing resource in the strings.xml file. XML error are listed in the Eclipse's console.
lemotdit
A: 

Simplest solution - Sometimes you just need to save the XML file you were working on to get the autogenerator to kick in.

Save the file (e.g. main.xml), and see if that resolves the R resolve problem

akim
A: 

I was having a similar issue. Thanks for the help

Usman Chaudhri
A: 

So I have run into this problem multiple times when switching build targets. Usually doing a Project >> Clean worked for me. This last time, however, it did not. Finally I tried to open my default.properties file, located under the root project folder. I received an error message stating that it was out of sync with the file system. I actually deleted it and copied a coworkers version which allowed eclipse to rebuild my R file. I will paste what it looks like below. It is named 'default.properties'.

# This file is automatically generated by Android Tools.
# Do not modify this file -- YOUR CHANGES WILL BE ERASED!
# 
# This file must be checked in Version Control Systems.
# 
# To customize properties used by the Ant build system use,
# "build.properties", and override values to adapt the script to your
# project structure.

# Indicates whether an apk should be generated for each density.
split.density=false
# Project target.
target=android-3
Matt Langston
A: 

Often times this is because of the MinSDK version number you supplied when creating the project. Example:

If you want 2.1 to be the minimum, Android 2.1 is actually API Level 7.

You can see what I am talking about when you browse the SDK you downloaded and installed. Navigate to the place you installed the SDK to (C:\android-sdk-windows for example) and open the folder named "platforms". You will see something like "android-7" listed as a folder, and if you open that there is a source.properties file that when opened with a text editor will show you the corresponding platform version.

When you create a project, and you must select a "Build Target" api, the last column in that list named "API Level" shows the number you are looking for when populating the MinSDK setting.

This is probably on of the most common mistakes that results in the R.java file not being created under Project > gen > packagename > R.java

Aaron DeRenard
A: 

Older question, but I just had this problem for the millionth time and realized what was causing it was that I created an xml file with uppercase letters in the name. All your xml filenames in /res must match [a-z0-9\._]

Tom Dignan
also, i just noticed stack overflow requires us to escape all backslashes.
Tom Dignan
A: 

Remove main.out.xml. I'm a noob and don't yet know what this file is used for, but removing it cleared the problem.

+5  A: 

After tracking down this problem as well, I found this note in the Android docs:

http://source.android.com/source/using-eclipse.html

Note: Eclipse sometimes likes to add an "import android.R" statement at the top of your files that use resources, especially when you ask eclipse to sort or otherwise manage imports. This will cause your make to break. Look out for these erroneous import statements and delete them.

While going through the Android sample tutorials, I would often use the Ctrl-Shift-O command to "Organize Imports" and generate any missing import statements. Sometimes this would generate the incorrect import statement which would hide the R.java class that is automatically generated when you build.

Michael Levy
This is indeed the correct answer.
Allen Gingrich
This guy needs more votes for this answer.
Mark Ingram
A: 

I had the same problem. In my case it was to do with the layout xml file names.

I had some of the file names with capitals in them.

screenQS.xml - didnt work!

screenqs.xml - worked!

Arturski
A: 

THANK YOU!!!!!!!!!!!!

asd
A: 

If you are on a mac, check if a .DS_Store has been added to a directory in /res (ls -al on Terminal command line.) Remove it and refresh/rebuild

mac user