全新安装 Eclipse,R 无法解析为变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14001974/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 19:40:52  来源:igfitidea点击:

Fresh install of Eclipse, R cannot be resolved to a variable

androidlinuxeclipsedevelopment-environmentadt

提问by feztheforeigner

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...

我无数次遇到“R无法解决”错误,但总是有一个简单的解决方法。很抱歉打扰你,但这次我很难过......

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.

我最近决定将我的计算机双启动到 Windows 7/Ubuntu(以前仅限 Windows 7)并为我的 Android 应用程序开发加载 Eclipse,但是我遇到了臭名昭著的 R 无法解决的错误。

After a clean install of Ubuntu 12.10, I set up Google's ADT Bundlefrom 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.

全新安装 Ubuntu 12.10 后,我从 Android Developer 网站设置了Google 的 ADT Bundle。我安装了上述下载包中包含的 Eclipse、SDK 和 ADB(ADT 已预先安装/设置)。这是谷歌提供的一个特殊版本的 Eclipse,在 Eclipse 启动时包含文本“Android Developer Tools”。

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".

Eclipse 运行 Java 项目很好,SDK 管理器似乎设置正确,因为我可以直接从 Eclipse 启动它(我还安装了所有的 Android SDK 1.5-4.2),AVD 管理器几乎可以正常工作(它启动并允许我创建新设备,但不会以消息“恐慌:无法打开:some_device”开头),但是任何 Android 项目都不会编译并出现错误“R 无法解析为变量”。

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.

我曾尝试使用我的两个旧工作区中现有的 Android 项目以及创建一个新工作区,但它们都诉诸于相同的错误。

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...

有什么建议吗?我真的很想开始在基于 Linux 的环境中工作,并花了几个小时试图让它工作......

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...

编辑:我开始认为这个问题可能与我在启动 Android 虚拟设备时出现的另一个错误有关,其中显示“恐慌:无法打开:some_device”。可悲的是,我不知道是什么原因造成的,也不知道如何解决它......

回答by fr4gus

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.

我遇到过同样的问题。全新安装 ADT Bundle,使用向导创建一个新项目,给我“R 无法解析为变量”错误。我尝试了上一篇文章中解释的所有内容。

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.

在我的特殊情况下,我有另一个 Android SDK 工具副本,而 ADT Bundle 指向的是那个,而不是捆绑包附带的那个。我只是改变了它,所有项目都重新开始工作。希望这可以帮助。

回答by Seven Yu

sudo apt-get install ia32-libs

回答by Sahil Mahajan Mj

Some quick solutions to your problem might be,

您的问题的一些快速解决方案可能是,

  • use the Ctrl+ Shift+ Ocommand to "Organize Imports" and generate any missing import statements. Sometimes this would generate the incorrect import statement which would hide the R.javaclass that is automatically generated when you build.
  • Ris 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.
  • 使用Ctrl+ Shift+O命令“组织导入”和生成所有缺少的import语句。有时这会生成不正确的导入语句,这会隐藏R.java构建时自动生成的类。
  • R是一个生成的类。如果您使用的是Android 开发工具 (ADT),则无论何时构建项目,它都会生成。您可能已关闭“自动构建”。打开它并构建您的项目。
  • xml 视图中的错误可能会导致无法生成 R.java。检查您的视图文件并确保所有 xml 都是正确的!
  • 删除,如果有 import android.R;
  • 重新启动您的 IDE(如果它刚刚安装)。

回答by Dipak Keshariya

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

有两种解决方案,它将解决您的问题。

1) Build your project.

1)构建你的项目。

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

2)转到项目>清理和清理你的项目。

回答by Chintan Rathod

All other answers are correct, but make sure that any layoutdon't have any error inside, otherwise eclipse will not generate R.javafile. So first try to solve any layouterrors and then apply Clean Projectand build one more time.

所有其他答案都是正确的,但请确保其中layout没有任何错误,否则 eclipse 将不会生成R.java文件。因此,首先尝试解决任何layout错误,然后再应用Clean Project并构建一次。

If error comes, remove genand bindirectory and one more time try to build project.

如果错误出现,删除genbin目录,并再一次尝试生成项目。

回答by mjanes

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.

我在 Eclipse 上遇到了这个问题,这是因为我删除了 bin 和 gen 目录,而 Eclipse 没有重新创建这些目录的权限。用 chmod 修复。

回答by user2449959

Make sure that all the SDKsare updated. In Eclipsego to Help -> Updatesand use the SDK Managerto make sure everything is updated.

确保所有SDK都已更新。在Eclipse 中,转到Help -> Updates并使用SDK Manager确保所有内容都已更新。

回答by user2449959

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

重新启动您的 IDE。R - 一种资源处理程序 - 将自动生成。