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!

Quarra
  • 2,527
  • 1
  • 19
  • 27
  • 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. – Hiren Dabhi Feb 16 '12 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. –  Feb 16 '12 at 14:09
  • @Deepak Not an exact duplicate. Also,at least please read the WHOLE question before downvoting it(anyone). – Fofole Feb 16 '12 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. – Quarra Feb 16 '12 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. – Deepak Feb 16 '12 at 16:29
  • It occured in my first project, and as you proposed I've created another one. Once again, _gen_ folder is empty, the same error. – Quarra Feb 17 '12 at 09:12

14 Answers14

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.

Blackbelt
  • 156,034
  • 29
  • 297
  • 305
  • 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? – Quarra Feb 16 '12 at 14:35
  • 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. – Blackbelt Feb 16 '12 at 14:39
  • main.xml file looks like that: Sorry, I don't know why it isn't displayed properly.. – Quarra Feb 16 '12 at 14:43
  • ";" a part it seems correct to me. Anything inside the drawable or values folder? – Blackbelt Feb 16 '12 at 14:47
  • Yes, in _drawable-hdpi_, _drawable-ldpi_, _drawable-mdpi_ there are images of the Android (named _ic_launcher.png_). Folder _values_ contains _strings.xml_ – Quarra Feb 16 '12 at 14:52
  • for instance if one of the png in your drawable folder has a capital letter R.java can not be generated because of "Invalid file name: must contain only [a-z0-9_.]". You have to check for this kind of error – Blackbelt Feb 16 '12 at 14:56
  • Files are named _ic_launcher.png_, lowercase characters only. – Quarra Feb 16 '12 at 14:59
  • it was only to make clear these error can prevent R.java to be generate. Check for illegal character in string.xml.. – Blackbelt Feb 16 '12 at 15:01
  • ` Hello World, main! First app ` I can't see any illegal charactes – Quarra Feb 16 '12 at 15:06
  • any issue with the AndroidManifest.xml file? – Blackbelt Feb 16 '12 at 15:10
  • No red-crosses, no illegal characters (which are: < and % I suppose) – Quarra Feb 16 '12 at 15:20
  • I give up.. as somebody alread told you look at the eclipse console and to the "problems" windows and check some clue there. Sorry – Blackbelt Feb 16 '12 at 15:29
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!

showlove
  • 26
  • 1
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;

edthethird
  • 6,263
  • 2
  • 24
  • 34
  • 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. – Quarra Feb 17 '12 at 09:16
  • 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` – edthethird Feb 17 '12 at 16:21
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.

davidcesarino
  • 16,160
  • 16
  • 68
  • 109
Dina
  • 11
  • 1
0

Make sure that you are extending Activity class

john
  • 359
  • 1
  • 4
  • 15
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

Community
  • 1
  • 1
Leo
  • 1
  • 1
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 :)

AJay
  • 1,233
  • 10
  • 19
0

Simple

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

Martin Koubek
  • 433
  • 4
  • 8
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.

Fofole
  • 3,398
  • 8
  • 38
  • 59
  • 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} – Quarra Feb 16 '12 at 14:02
  • Try go to main.java press ctrl+shift+O and import the right R. Dunno if this works but worth trying. – Fofole Feb 16 '12 at 14:14
  • 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. – Quarra Feb 16 '12 at 14:23
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!

Vikram Singh
  • 1,726
  • 1
  • 13
  • 25
0

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

Nitin Bansal
  • 2,986
  • 3
  • 23
  • 30
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.

AndroDev
  • 3,236
  • 8
  • 35
  • 49
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.

AndroDev
  • 3,236
  • 8
  • 35
  • 49
  • I've tried it. _R.java_ hasn't been generated, therefore the error hasn't disapired. – Quarra Feb 17 '12 at 09:39
  • Did you download Android SDK properly? Just check whether aapt.exe is available in android_sdk/plateform tools folder. – AndroDev Feb 17 '12 at 09:47
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!

Quarra
  • 2,527
  • 1
  • 19
  • 27