问题
In Eclipse, I\'ve created a project from a source and now it shows errors - \"R cannot be resolved to a variable\". From what I found here, I had cleared and rebuilt the project, but still the R file doesn\'t appear in the /gen folder.
Any ideas?
回答1:
Dont worry. First you may clean the project, then run the project. If this does not work then follow the following links:
- Here is the best way to solve this problem: [Android Development- Where is my R.Java file?][2]
- R.java not regenerating
- R cannot be resolved - Android error
- R cannot be resolved to a variable
- R cannot be resolved to a variable -- mailing list entry
- Fixed: R cannot be resolved to a variable
回答2:
If Clean/Rebuild Project doesn't work try to check our package name in AndroidManifest.xml.
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.
回答3:
For me the error got fixed by making some changes in Android SDK Manager.
Whatever be the latest API level available, install its "SDK Platform". For me latest API level available was 16, so I installed its's SDK Platform as shown in the image below. It works fine now.

Cheers, Mayank
回答4:
Did you just update both sdk and adt(from 21 to 22), then you need to install a new item: Android SDK Build-tools
Refer to: Eclipse giving error, missing R.java file after recent update
回答5:
For me somehow the Project properties; Android; Project Build Target was not set. I chose a Android version there (e.g. 4.2) and it fixed it.
回答6:
for me, the best solution to suggest to people that have problems with the "R" file would be to try the next steps (order doesn't matter) :
update ADT & SDK , Eclipse and Java.
remove gen folder , and create it again .
do a clean-project.
right click the project and choose android-tools -> fix-project-properties .
right click the project and choose properties -> java-build-path -> order-and-export. make sure the order is :
Android 4.3 (always the latest version)
Android private libraries
android dependencies
your library project/s if needed
yourAppProject/gen
yourAppProject/src
make sure all files in the res folder's subfolders have names that are ok : only lowercase letters, digits and underscore ("_") .
always make sure the targetSdk is pointed to the latest API (currently 18) , and set it in the project.properties file
回答7:
For me, there was a missing slash at the end of my SDK. In Eclipse > Preferences > Android > SDK Location
For example, change
/home/matt/android-sdk-linux
to
/home/matt/android-sdk-linux/
回答8:
I got similar problem and solution I found out was in resources any of the file aka background, sound etc must not contain capital letters or any special symbol other than _
回答9:
your android manifest must start with correct package
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="HERE - Correct package NAME"
android:versionCode="1"
android:versionName="1.0" >`
回答10:
What you really need to do it just install "build-tools" from sdk manager, and r.java will be generated automatically.
回答11:
I had the same error. Error was that, I had placed a file in res/raw folder with invalid filename. As soon as I corrected the file name to a valid one, error was resolved!
Allowed characters for naming a file:-
1. a-z
2. . _
3. 0-9
4. no capital letters
Capital letters were the issue in my case!
回答12:
Sometimes you can accidentially add an import to "R", so if at the top of your code you see some weird import about that that you did not add yourself, delete the reference, and everything should go back to normal
回答13:
My problem was strange and took some time to find. Somehow the package of the src file changed so that the last entry in the package was deleted. So for example initially my class MyActivity.java was in package com.abc.client.test.app but after I added a user permission, the app got removed and the package was renamed to com.abc.client.test. I don't know how it happened. Renaming the package and putting the java file in the correct place fixed the problem.
回答14:
I found this problem when trying to run the Notepadv3 programme from Googles 'first lesson'..
For me it was an issue with the xml file, due to the API level I was using. I renamed each case of Match_Parent to the older type of Fill_Parent. Oh and if you have already auto-built the project then you need to delete 'import android.R' on NoteEdit.java and Notepadv3.java, clean the project (click 'Project', then 'clean...') before saving.
Solved it for me. Came from this post Android NDK r4 san-angeles problem
Can't believe Google dosen't warn of this problem - I have been setting up another PC to start programming again and even with a bit of prior knowledge this was a pain.. How do you set the API when importing an existing project??? Can't see where there is any dialogue option when you 'File' 'Import' etc ..
Scamparelli
回答15:
Check the androidmanifest.xml
file and layoutfolder xml files. They should be created properly as the starting and ending clause in xml should be placed properly. Update the files, clean and build. And all set!
回答16:
I had this problem and none of the other guides helped, and then I realized I didn't have the java jdk installed on my system. If you haven't done this either go download the version corresponding to the version of eclipse you installed (x86 or x64)
回答17:
It is possible you have an error in your *.xml files: layouts and etc.
回答18:
Try to modify AndroidManifest file.
For example add space and delete this space. After this OPERATION, save project. Resources will be refreshed. It can help.
回答19:
I solved the problem with resolving R resource on fc19.x86_64 and ADT v22.0.5-757759 by installing additional libraries after Fedora and ADT upgrade.
yum install ld-linux.so.2
yum install libstdc++.so.6
yum install libz.so.1
Those libraries are required by adb (Android Debug Bridge version 1.0.31).
Then restart Eclipse and perform project clean Project->Clean check project you would like to clean.
Hope it helps :)
回答20:
Project > Clean...
usually regenerates R.java file located in gen/com.mypackage
回答21:
In my case, R.java wasn't getting generated because of a hierarchical parent error, which in turn was cured by updated one or more Eclipse plugins (Go Eclipse-->Help-->Check for Updates)
回答22:
I got this error when I added a js file to the res folder.
An error indicator was set on the res folder icon. In all the classes where I used R I got this error.
I moved the js file to the assets folder and everything worked.
回答23:
Problem was eclipse was not generating R.java. To resolve this issue please go to project->clean... and select your project and select ok and then clean and build project and import your package name R file(Ex com.demo.R). It works.
回答24:
This can happen when the android naming convention isn't followed. Check if you have placed a file/photo into one of the android folders and make sure that the file name contains only lower case letters
回答25:
I know this is an old question but I just solved my own version of it and perhaps this might help someone.
After two days of tearing my hair out with this, I finally got around it by deleting the raw
folder, then recreating it and dropping the file(s) back in.
After that, another Project > Clean
and it at last compiled.
回答26:
I got this when I renamed a project. Although the project is renamed, some references are not. Clearing lint, and then rerunning lint helped. (Right click project > Android Tools > Clear Lint Markers, then Run Lint)
回答27:
My setup was broken after a recent update where the SDK build tools need to be installed separately after the SDK installation. So build tools update + Eclipse restart fixed this for me.
Credit goes to the Android reply here
回答28:
When other solutions fail, select your project and delete it. MAKE SURE YOU DO NOT REMOVE FILES FROM DISK.
Then use file>import>existing android code into workspace, and select the location of your project.
回答29:
I been having this and i did trace it back to the layout files.. It happend to me again however i could not find noting in the layout files. I looked in the string.xml and there was it.. one of the string names started with cap and that was causing the problem.
回答30:
Fix any XML formatting errors in the XML Files in your /res/menu folder.
Might be a compile time error from the XML file being improperly formatted.
来源:https://stackoverflow.com/questions/7824730/r-cannot-be-resolved-to-a-variable