0

I have an issue that many people had, looking at the numerous questions, which is described in the subject : sometimes, R.java disappears and after some (far too long) minutes of changing back what I did, R.java comes back. When R.java isn't generated, lines calling for R show the error : 'R cannot be resolved to a variable'.

I know that R isn't the real problem, and that there is one or more error somewhere in my code.

My question is : how to localize these errors, provided Eclipse doesn't show them?

If you have the same problem as me, first see "R cannot be resolved to a variable"? which can help you getting rid of that mercyless error

Community
  • 1
  • 1
WhiskThimble
  • 547
  • 3
  • 10
  • 23
  • 2
    Try cleaning your project... most of the times this happens because of project builds. – Anurag Shukla Feb 27 '13 at 14:53
  • "R.java disappears ... comes back". i haven't got the slightest idea what you are meaning here. can you enlight me? – bofredo Feb 27 '13 at 14:55
  • I have already tried to clean my project, as hinted there http://stackoverflow.com/questions/7824730/r-cannot-be-resolved-to-a-variable But R isn't generated because of an error in my code, which I can't localize – WhiskThimble Feb 27 '13 at 15:31
  • R.java is an autogenerating file, that Eclipse makes from the code (mainly resources) you wrote. Everytime you change your code, if 'Build automatically' is activated (it is by default), R.java is deleted and re created by Eclipse. This file is needed to start your project. In my case, the file is deleted, but can't be re generated – WhiskThimble Feb 27 '13 at 15:35

8 Answers8

4

If you're trying to pinpoint the source of this error, try this:

  1. Go to Project -> Clean. This will ensure that your error wasn't because of a bad build. If R can still not be found, move onto step 2.
  2. You should have a Problems tab in Eclipse which will list out all the errors and warnings in open projects, as shown in the screenshot below. If you can't see the Problems tab, go to Window -> Show View -> Problems
  3. Expand the Errors section to see what problems are present. You will be told the problem, the project, the file and the line number in most cases.

enter image description here

Raghav Sood
  • 81,899
  • 22
  • 187
  • 195
  • First, thanks for your help. I am new a new user of Eclipse and I didn't know the Problems tab. But my problem is : the error shown in this tab is 'R cannot be resolved to a variable', which indicates that R isn't generated. I would like to find the error that prevents Eclipse from generating R.java – WhiskThimble Feb 27 '13 at 15:40
  • 1
    If R.java is not being generated, then you have a problem in your XML files or Android manifest or project build path. The problems tab *should* list these as well, but if it doesn't, check the file navigator on the left hand side for red crosses on folders or files. – Raghav Sood Feb 27 '13 at 15:57
  • As you say, ''should'', but in my case not. – Didac Perez Parera Jul 18 '13 at 13:37
2

how to localize these errors, provided Eclipse doesn't show them?

Usually, Eclipse does show them. The errors will be in a resource or your manifest, usually indicated by a red X badge over the icon of the file in the Package Manager. Usually, the Console will contain a log message about the error as well.

Push come to shove, you can do a command-line build and see what that reports.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • As I was saying to Raghav Sood, the error log shows me the error 'R cannot be resolved to a variable', which isn't the real error I am looking for – WhiskThimble Feb 27 '13 at 15:50
  • @WhiskThimble: I do not know what you consider "the error log" to be. Errors that lead to `R` problems usually show up in the Eclipse Package Manager (red X badges) and the Eclipse Console, as I indicated in my answer. – CommonsWare Feb 27 '13 at 16:45
  • @DídacPérez: If you take the time to actually read the answer, you might consider following the instructions in the answer, including looking at the Console and running the command-line build. – CommonsWare Jul 18 '13 at 13:40
1

Eclipse won't build the R.java file if there's somethin wrong in your /res folder or other XML files.

I recommend you to check that folder.

Raghav Sood
  • 81,899
  • 22
  • 187
  • 195
VinceFR
  • 2,551
  • 1
  • 21
  • 27
0

If clean does not work and there is no error other than missing R.java restart the eclipse.

File Menu->restart

Fred F
  • 1,027
  • 1
  • 9
  • 18
  • It's not a problem about Eclipse (or so I think it isn't). Clean seems to work pretty well, but R.java isn't regenerated. In case you would like to know, I restarted it anyway, but nothing changes ;-) – WhiskThimble Feb 27 '13 at 15:54
0

Found the issue, but can't figure out how to find it simply. I accenditally erased this line < string name="action_settings">Settings< /string> while doing copy past of another one.

Don't know what it is used for, but Eclipse didn't show this in error log, nor anywhere I looked for.

Thanks anyway guys for the help, hope I won't have some similar problem again!

WhiskThimble
  • 547
  • 3
  • 10
  • 23
0

1) see if the xml files in the Package Explorer tree have an error-icon if they do, fix errors and R-class will be generated.

example hint: FILL_PARENT was renamed MATCH_PARENT in API Level 8; so if you're targeting platform 2.1 (api 7), change all occurences back to "fill_parent"

2) if you added "import android.R;" trying to do a quick-fix, remove that line

3) Check your AndroidManifest.xml file, see if the package name is correct. If you copied an example it is likely this is wrong and the source of your problem

4) I had the same problem. I didn't have any lines stating "import something.R". Cleaning and rebuilding also didn't solve the issue.

It turned out to be because of the name of a layout xml file. Those files can only contain lowercase letters, or numbers, or underscores. When I changed the filename (and everything that refers to it) from myLayout.xml to my_layout.xml, everything worked.

5) I have just had this problem. It was caused because I had a redundant menu resource that referred to a non-existing string. Removed the menu XML and back came R.

0

As has been said in this link "R cannot be resolved to a variable"? and this other R cannot be resolved - Android error there are several solutions.

In my case I configured proxy server in SDK Manager: Tools> Options

Community
  • 1
  • 1
Ednilson Campos
  • 187
  • 2
  • 8
0

i was getting this error, after i updated the sdk tools and packages. i think the update hadnt worked properly, after a lot of head scratching, i just tried updating the sdk packages again using the sdk manager tool, and after that everything was ok.

user280109
  • 1,476
  • 3
  • 16
  • 27