Fresh install of Eclipse, R cannot be resolved to

2020-06-16 09:15发布

问题:

I have come across the "R cannot be resolved" error countless times but there has always been an easy fix. Sorry to trouble you but I'm stumped this time...

I recently decided to dual boot my computer into Windows 7/Ubuntu (previously Windows 7 only) and load Eclipse for my Android app development, however I came across the infamous R cannot be resolved error.

After a clean install of Ubuntu 12.10, I set up Google's ADT Bundle from the Android Developer website. I installed the Eclipse, SDK and ADB that were included in the package from the above download (ADT came pre-installed/setup). This is a special version of Eclipse provided by Google and includes the text "Android Developer Tools" when Eclipse is started.

Eclipse runs Java Projects fine, the SDK Manager seems to be set up correctly because I can launch it directly from Eclipse (I have also installed all of Android SDKs 1.5-4.2), AVD Manager almost works correctly (it launches and allows me to create new devices but will not start with the message "PANIC: could not open: some_device"), however any Android project will not compile with the error "R cannot be resolved to a variable".

I have tried using my existing Android projects from two of my old workspaces as well as creating a new workspace but they all resort to the same error.

Any advice? I would really like to begin working in a Linux-based environment and have spent several hours trying to get this to work...

I would highly appreciate anyone's ideas/advice/solutions/pity/etc.

EDIT: I'm starting to think this problem is probably connected to my other error when launching an Android Virtual Device where it states "PANIC: could not open: some_device". Sadly I have no idea what's causing this nor how to fix it either...

回答1:

I had the same issue. Fresh install of ADT Bundle, creating a new project using wizard, was giving me "R cannot be resolved to a variable" error. I tried everything explained in previous post.

In my particular case, it was that I had another copy of Android SDK tools and ADT Bundle was pointing to that one, instead of the one that comes with the bundle. I just changed that, and all projects were working again. Hope this helps.



回答2:

sudo apt-get install ia32-libs


回答3:

Some quick solutions to your problem might be,

  • 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.
  • R is a generated class. If you are using the Android Development Tools (ADT) it is generated whenever the project is built. You may have 'Build Automatically' turned off. Turn it on and build your project.
  • Mistakes in your xml views could cause the R.java not to be generated. Go through your view files and make sure all the xml is right!
  • Remove, if any import android.R;
  • Restart your IDE, if it is just installed.


回答4:

There are two solution for this, it will solve your problem.

1) Build your project.

2) Go to Project > Clean and the Clean your project.



回答5:

All other answers are correct, but make sure that any layout don't have any error inside, otherwise eclipse will not generate R.java file. So first try to solve any layout errors and then apply Clean Project and build one more time.

If error comes, remove gen and bin directory and one more time try to build project.



回答6:

I have had this issue with Eclipse, and it was because I had deleted the bin and gen directories, and Eclipse did not have permissions to recreate those directories. Fixed with chmod.



回答7:

Make sure that all the SDKs are updated. In Eclipse go to Help -> Updates and use the SDK Manager to make sure everything is updated.



回答8:

Restart your IDE. R - kind of resource handler - will be auto generated.