12

I've got an error:

R cannot be resolved to a variable

in line:

setContentView(R.layout.main);

but I can't resolve it doing things that other users wrote should be tried, which are:

  1. Getting rid of 'import android.R' line.
  2. Cleaning the project and then building it again.
  3. Closing project and then opening it again.
  4. Going to Project Properties > Java Build Path > Tick Android Version Checkbox
  5. Following the procedure: Uncheck Project->Build Automatically Project->Properties->Builders: Make sure all Android (3) and Java (1) builders are checked (and nothing more) Window->Preferences->Android->Build: Check "Automatically refresh Resources and Assets folder on build" Right-click on project->Android Tools->Fix Project Settings (not sure what it actually does, but it can't hurt)
  6. Some users advice to check if .xml files are correct. I don't know how I could do that. I must admit I'm new to java and android programming, and I haven't even written a single line in my project - the code has been generated automaticly.
  7. Layout xml file is named using lower-case letters.
  8. No error in resource files (no red-crosses).

layout main.xml file:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

</LinearLayout>

Thanks in advance for your help!

6
  • check in your XML files in that might be you uses resource that is not available in your resources.because it some time not show you error but when you clean and build the project is not built successfully. Commented Feb 16, 2012 at 13:46
  • 7
    2 Downvotes? Seriously? Did you only read the title of the question? He clearly did a lot of research and checked things beforehand. This question is clear and has enough details for starters. Everything mentioned in the thread linked in the comments is already done. And all the lazy questions with ZERO reseach effort get upvoted to the sky. I don't get you at times, SO community. Commented Feb 16, 2012 at 14:09
  • @Deepak Not an exact duplicate. Also,at least please read the WHOLE question before downvoting it(anyone). Commented Feb 16, 2012 at 14:15
  • I'm just going crazy as I'm trying to figure it out for two days now. It's hard to mention ALL things I've already tried... I've seen the above-mantioned post. I've pointed out the most common resolutions. Commented Feb 16, 2012 at 14:18
  • Is this problem only in your current project or also in new created projects too ? Try to recreate your project and check this and please notify me if this problem get solved or not. Commented Feb 16, 2012 at 16:29

14 Answers 14

5

Check your res/ folder in order to be sure that every file it contains has no error. If any error exists R can not be generated.

Sign up to request clarification or add additional context in comments.

12 Comments

Is it possible that no red-cross occurs if a file contains an error? Maybe there is a problem with res folder, but I can't see it for some reason?
dunno it is possible or not but remember that R.java is automatically generating from the res folder content (R.java contains your layout id, for instance). So, my best guess is the problem is there.
main.xml file looks like that: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> </LinearLayout> Sorry, I don't know why it isn't displayed properly..
";" a part it seems correct to me. Anything inside the drawable or values folder?
Yes, in drawable-hdpi, drawable-ldpi, drawable-mdpi there are images of the Android (named ic_launcher.png). Folder values contains strings.xml
|
1

Yeah, i got stuck on this for hours. My solution was to: delete all projects from workspace->re-import the desired project by going to file->import->Browse->selecting the project->and finally check the "add project to working sets box" then clicking finish. The .R errors i once had then disappeared. I hope this helps someone!

Comments

1

Don't import Android.R;

instead,

import package.name.R; where package.name is your package name declared in the manifest.

Android.R contains all the default layouts packaged with Android. Your custom layout will not be Android.R, it will be in package.name.R;

2 Comments

I tried this also. I've got another error in addition to the first one: The import my.second.project.R cannot be resolved I suppose it's not a solution.
if R isn't being generated, there is a problem with one of your resources. Check out your layouts and drawables and manifest, make sure nothing is marked red. Once R is generated, you want to make sure you are referencing my.projet.R instead of Android.R
0

On eclipse you have the "Problems" window. If you fix all the other problems in there other than R cannot be resolved, the "R" problem will be auto-fixed as R is not generated because most likely you have project related problems.

Hope this helps. If you don't manage to do it, put a print of what your Problems window is showing.

3 Comments

I can't upload an image (insufficient raputation points). I've got one and only problem in 'Problems' window: {R cannot be resolved to a variable main.java}
Try go to main.java press ctrl+shift+O and import the right R. Dunno if this works but worth trying.
Well, I'm not an expert, but other users don't recommend it as it generates import android.R which isn't the issue here.
0

Clean your project once. It should build that file again automatically. Go to Project->Clean Of course, it will work ony if your xml files are syntactically correct!

1 Comment

It would be nice reading the actual question in which he states he cleaned the project.
0

Delete your R.java fie and let it generate again...possibly this will help...i had same issue multiple times

1 Comment

I didn't have a R.java file in the first place.
0

Try this: comment setContentView(R.layout.main); and the build/clean your project again. Then uncomment the commented line. This issue should not be seen then.

1 Comment

Tried this one. No effect - still the same problem.
0

Try this:

  1. First disable automatic build option from Project>Build Automatically
  2. Change eclipse work space
  3. Create a new Hello World test project
  4. Comment setContentView(R.layout.main);
  5. Build project manually by right click on your project in eclipse>Build project
  6. Check whether R.java in gen folder has been generated
  7. If yes, then uncomment setContentView(R.layout.main);
  8. Now build project again manually and run

This should resolve the problem hopefully.

2 Comments

I've tried it. R.java hasn't been generated, therefore the error hasn't disapired.
Did you download Android SDK properly? Just check whether aapt.exe is available in android_sdk/plateform tools folder.
0

Problem is solved, but I don't know what has changed... I reinstalled Eclipse, SDK etc. and gen folder was empty as before, the same problem.

My supervisor told me to do it all again, and it worked (for now). I don't know what was the cause. I though I'd had tried everything, but it's working now so I'll never know. Thanks for your help guys!

Comments

0

Nothing mentioned above worked for me. However, I fixed it myself.

Solution:

If you've any other files except activity_main.xml / main.xml inside app_name/res/layout remove it and try clean and build.

Note:

The unwanted files or (rather used later) files inside app_name/res/layout would be *.out.xml. Remove the same.

Comments

0

Make sure that you are extending Activity class

1 Comment

Please always elaborate your answer
0

I've had a similar problem. Tried everything and gen folder remained empty. It had to be with the SDK version 22. I hadn't installed the "Android SDK build-tools". I just had to install it with SDK Manager and restart Eclipse. As a member pointed on another post, the answer is better explained in this link: https://stackoverflow.com/a/16643060/1691231

Comments

0

What you really need to do it just install "build-tools" from sdk manager, and r.java will be generated automatically in gen folder :)

Comments

0

Simple

Check that you have the same SDK - build version with your AndroidManifest and project.properties version.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.