R 无法解析 - Android 错误
我刚刚下载并安装了新的 Android SDK。 我想创建一个简单的应用程序来测试它。
向导创建了此代码:
package eu.mauriziopz.gps;
import android.app.Activity;
import android.os.Bundle;
public class ggps extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
但是 Eclipse 给了我错误
R无法解析
R无法在线
setContentView(R.layout.main);
为什么?
PS:我在 res/layout/
下确实有一个名为 main.xml
的 XML 文件。
I just downloaded and installed the new Android SDK. I wanted to create a simple application to test drive it.
The wizard created this code:
package eu.mauriziopz.gps;
import android.app.Activity;
import android.os.Bundle;
public class ggps extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
but Eclipse gives me the error
R cannot be resolved
on line
setContentView(R.layout.main);
Why?
PS: I do have an XML file named main.xml
under res/layout/
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(30)
在追踪到这个问题之后,我在Android文档中找到了这个注释:
http:// source.android.com/source/using-eclipse.html
在浏览 Android 示例教程时,我经常使用 Ctrl + Shift + O 命令来“组织导入” " 并生成任何缺失的导入语句。 有时这会生成不正确的导入语句,从而隐藏构建时自动生成的
R.java
类。After tracking down this problem as well, I found this note in the Android documentation:
http://source.android.com/source/using-eclipse.html
While going through the Android sample tutorials, I would often 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 未生成甚至消失的问题时,都是由于 XML 布局文件中的某些问题导致应用程序无法构建。
Each time I had a problem with R not been generated, or even disappeared, this was due to some problem in the XML layout file that prevented the application from being built.
每当你得到
,然后检查
/res
目录,并且一定有某个文件存在某些错误,并且导致应用程序无法构建。 例如,它可能是一个布局文件,也可能是由于缺少某些资源,但您已经在 XML 文件中定义了它。如果像 res/drawables-mdpi 这样的文件夹中有任何额外的、甚至未使用的 (!) 或未引用的 (!) 图像,这些图像不符合文件命名约定(可能仅包含 [a-z0-9_.]),则R.java 类可能不会生成,从而导致所有其他帖子提到的事件链。
Whenever you get
then check for the
/res
directory and there must be some file that have some error in it and that is preventing the application from being built. For example, it may be a layout file or it may be due to some missing resource is, but you already defined it in the XML file.If you have any additional, even unused (!) or unreferenced (!) images in a folder like res/drawables-mdpi which do not comply to the file naming conventions (may contain only [a-z0-9_.]), the R.java class might not generate, causing the chain of events all the other posts referred to.
看看你的进口。 很可能会有这样一行:
将会在那里。 如果是这种情况,请将其删除,以便您的项目不会使用默认的 Android 资源类来解析 R,而是使用从 /res/ 文件夹自动生成的类来解析 R。
Look at your imports. Chances are that the line:
will be there. If that's the case, remove it, so that your project will resolve R not with the default Android Resources class, but with the one auto-generated from your /res/ folder.
另一件可能导致此问题的事情是:
我安装了新的 ADT(v.22)。 它停止创建包含
R.java
的gen
文件夹。 解决方案是还从 Android SDK Manager 安装新的 Android SDK 构建工具。此处找到了解决方案
And another thing which may cause this problem:
I installed the new ADT (v. 22). It stopped creating
gen
folder which includesR.java
. The solution was to also install new Android SDK Build Tools from Android SDK Manager.Solution found here
威尔说的是对的
但我发现还有另一个问题导致了第一个问题。 SDK目录下的tools没有执行权限,Eclipse就像不存在一样,没有构建
R.java
文件。因此修改权限并选择“自动构建”解决了问题。
What Will said was right
but I've found out that there was another problem that was causing the first one. The tools in the SDK directory didn't have the permissions to be executed, so it was like the didn't exist for Eclipse, thus it didn't build the
R.java
file.So modifying the permission and selecting "Build Automatically" solved the problem.
R.java
是 Android Eclipse 插件在创建时创建的文件构建您的应用程序。
R.java
在“gen”下创建目录。 该文件是根据“res”中的信息生成的
目录。 如果运行选择“Project”-> 日食号上的“干净……”
菜单,它将删除然后重新生成
R.java
文件。当你改变你的
AndroidManifest.xml
文件中的包名称。 它使用您的 Android包名称在“gen”目录下创建子目录,其中
它存储 R.java 文件。
Eclipse 执行 clean 时可能会出现问题,因为它对以下内容感到困惑
当您更改 Android 包时,
R.java
文件所在的位置姓名。 您可以重命名
gen
下的子目录以匹配您的新的包名称,或者您可以将包名称更改回旧的
姓名。 进行清理,然后将包名称更改为新名称
你要。 如果您阻止 Eclipse 尝试构建,则效果最佳
当您更改包名称时。 在“项目”菜单下
取消选中“自动构建”选项,并且当
“清理...”对话框询问是否应该“立即开始构建”
取消选中该框,以便在您更改时它不会尝试构建
包裹名字。 更改名称后,您可以将“Build
自动”重新打开。
请注意,如果您的
AndroidManifest.xml
文件包名称不匹配你的 Java 包名称,Eclipse 最终会自动添加一个
“导入<您的Android包名称>.R;” 所有
.java
文件中的行对 R 有任何引用。如果您更改
AndroidManifest.xml
包名称,有时 Eclipse 不会更新所有这些添加的内容
进口。 如果发生这种情况,请使用 Eclipse 重构 (ALT +
Shift + R)将 Java 文件之一中的导入语句更改为
您的新
AndroidManifest.xml
包名称。 最好这样做当您禁用“自动构建”时。
R.java
is a file that the Android Eclipse plugins creates whilebuilding your application.
R.java
is created under the "gen"directory. This file is generated from the information in the "res"
directory. If you run select "Project" -> "Clean..." on the Eclipse
menu, it will remove and then regenerate the
R.java
file.The problem "R cannot be resolved" happens when you change your
package name in the
AndroidManifest.xml
file. It uses your Androidpackage name to create a subdirectory under the "gen" directory where
it stores the
R.java
file.Eclipse may have problems executing clean, because it is confused about
where the
R.java
file is when you have changed the Android packagename. You can either rename the subdirectory under
gen
to match yournew package name, or you can change your package name back to the old
name. Do the clean and then change the package name to the new name
you want. This works best if you stop Eclipse from trying to build
while you are changing the package name. Under the "Project" menu
uncheck the option to "Build Automatically" and also when the
"Clean..." dialog asks if it should "Start a build immediately"
uncheck the box so it doesn't try to build while you are changing the
package name. After you have changed the name you can turn "Build
Automatically" back on again.
Note that if your
AndroidManifest.xml
file package name does not matchyour Java package name, Eclipse will end up automatically adding an
"import <your Android package name>.R;" line in all your
.java
filesthat have any references to R. If you change your
AndroidManifest.xml
package name, sometimes Eclipse does not update all of these added
imports. If that happens, use the Eclipse refactoring (ALT +
Shift + R) to change the import statement in one of your Java files to
your new
AndroidManifest.xml
package name. It is best to do thiswhile you have disabled "Build Automatically".
R 是一个自动生成的类,其中包含用于标识资源的常量。 如果您没有 R.java 文件(使用 1.5 SDK 的 Eclipse 中的文件为 gen/eu.mauriziopz.gps/R.java),我建议您关闭并重新打开您的项目,或者转至 Project > R.java。 全部构建(并按照约瑟夫的建议选择“自动构建”)。 如果这不起作用,请尝试创建一个新项目,如果问题再次出现,请再次在此处发布,我们将进行更详细的介绍。
R is an automatically generated class that holds the constants used to identify your resources. If you don't have an R.java file (it would be gen/eu.mauriziopz.gps/R.java in Eclipse with the 1.5 SDK) I would recommend closing and reopening your project or going to Project > Build all (and selecting "Build Automatically" while there as recommended by Josef). If that doesn't work than try making a new project, if the problem is recreated than post here again and we'll go into more detail.
关闭所有文件,清理项目,重新启动 Eclipse。
Close all files, clean project, restart Eclipse.
值得检查
AndroidManifest.xml
。 属性package
具有正确的值。也就是说:
更改后,
R.java
将重新生成。It is worth checking in
AndroidManifest.xml
. The attributepackage
has the correct value.That is:
After you change that, the
R.java
will be re-generated.此错误也可能是由于将活动添加到与包的根命名空间不同的命名空间而引起的。
例如,如果
com.example.myapp
是您的包的根命名空间,则您可以将活动添加到com.example.myapp.activities
命名空间。这将产生“R 无法解析”错误。
要修复活动中默认命名空间中的导入,R 应该是:
This error can also be caused by adding an activity to a namespace that is different to the root namespace for your package.
For example, if
com.example.myapp
is the root namespace for your package, you can then add an activity to thecom.example.myapp.activities
namespace.This will produce the "R cannot be resolved" error.
To fix the import the R in the default namespace in your activity should be:
除了前面答案中的建议外,请确保设置了您的 Android 目标:
编辑:一年后我找到了另一个原因。 我的可绘制文件夹中有一个 .jpg 图像,其名称与 .png 图像相同。 在我的代码中引用此图像一定会混淆程序,并给出“R 无法解析”错误。
Along with the great suggestions in the previous answers, make sure your Android target is set:
Edit: A year later I found another cause. I had a .jpg image in my drawable folder with the same name as a .png image. Referencing this image in my code must have confused the program and it gave the "R cannot be resolved" error.
确保您已从 sdk 管理器
项目中右键单击“属性”->“ 安装了 Android 构建工具” Java BuildPath 选择 Library 并添加 android-support.jar,按照以下步骤操作。
转到“项目”->“属性”->“Java 构建路径”,然后选择“订单和导出”选项卡。 将 android-support .jar 库设置为选中并将其添加到列表顶部。 并清理和重建..它适用于大多数情况
Make sure you installed the Android build tool form sdk manager
project right click properties-> Java BuildPath select Library and add android-support.jar the follow these step.
Go to Project->Properties->Java Build Path than select Order and export tab. Set android-support .jar library checked and up it into top of the list. And clean and rebuild..It works for most of the cases
我刚刚第一百万次遇到这个问题,并意识到是什么原因造成的:我创建了一个名称中包含大写字母的 XML 文件。
/res
中的所有 XML 文件名必须与[a-z0-9\\._]
匹配。I just had this problem for the millionth time and realized what was causing it: I created an XML file with uppercase letters in the name. All your XML filenames in
/res
must match[a-z0-9\\._]
.最简单的解决方案 - 有时您只需要保存正在处理的 XML 文件即可启动自动生成器。
保存文件(例如 main.xml),然后删除 R.java 文件并查看重新生成的 R.java 是否可以解决该问题R 解决问题。
Simplest solution - Sometimes you just need to save the XML file you were working on to get the autogenerator to kick in.
Save the file (e.g. main.xml) then delete the R.java file and see if the regenerated R.java resolves the R resolve problem.
检查 XML 文件名。 确保它们全部为小写。
还要确保所有图像资源名称也全部为小写。 我的 jpg 文件名中有一个大写字母,它导致我的项目出现 R unresolved 错误。
Check the XML file names. Be sure that they're all in lowercase.
Also make sure that any image resource names are also all in LOWER CASE. I had a capital letter in the name of my jpg file, and it caused the R unresolved error right across my project.
R
是一个生成的类。 如果您使用 Android 开发工具 (ADT),则每当该项目已建成。 您可能已关闭“自动构建”。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.我的 x64 Linux Mint 安装中出现了此错误。 事实证明,结果是
ADB
二进制文件失败,因为ia32-libs
软件包没有安装。 只需运行 apt-get install ia32-libs 并重新启动 Eclipse 即可修复该错误。如果您的 x64 发行版没有 ia32-libs,则您必须使用 Multiarch。
检查这篇文章的#4 和 #5:
http://crunchbang.org/forums/viewtopic.php?pid=277883#p277883
希望这对某人有帮助。
This error cropped up on my x64 Linux Mint installation. It turned out that the result was a failure in the
ADB
binary, because theia32-libs
package was not installed. Simply runningapt-get install ia32-libs
and relaunching Eclipse fixed the error.If your x64 distro does not have ia32-libs, you'll have to go Multiarch.
Check #4 and #5 on this post:
http://crunchbang.org/forums/viewtopic.php?pid=277883#p277883
Hope this helps someone.
我也有这个问题。 事实证明,我无意中从
strings.xml
文件中删除了“app_name”字符串资源,这导致了无提示错误。 一旦我将其添加回来,R 类就已成功生成,并且一切都已恢复并运行。I had this problem as well. It turned out that I had inadvertently deleted the "app_name" string resource from the
strings.xml
file, which was causing a silent error. Once I added it back, the R class was generated successfully and everything was back up and running.您可能需要更新 SDK 工具。 再次重新启动 Android SDK Manager 并安装一个新项目:Android SDK Build-tools。
You may need to update SDK tools. Relaunch Android SDK Manager again and install a new item: Android SDK Build-tools.
您可能需要更新/安装 SDK 工具。 再次重新启动Android SDK Manager并安装一个新项目:Android SDK Build-tools。一一删除,修复哪个适合您。
You may need to update/install SDK tools. Relaunch Android SDK Manager again and install a new item: Android SDK Build-tools.one by one delete,fix which one work for you.
尝试将新的 XML 布局文件名设置为小写。 例如,使用
my_file.xml
而不是myFile.xml
。Try to make your new XML layout file name lower case. For example, use
my_file.xml
instead ofmyFile.xml
.R.java
可能无法自动生成的另一个原因是,如果您有 res/drawable-hdpi、res/drawable-mdpi 等目录,或者res/drawable-ldpi。1.6+ 似乎可以使用这些目录,但 1.5 不需要它们。 当我删除这些目录时,
R.java
再次开始为我自动生成。Yet another reason
R.java
might not get autogenerated is if you have directories like res/drawable-hdpi, res/drawable-mdpi, or res/drawable-ldpi.1.6+ seems to be OK with these directories, but 1.5 doesn't want them. When I removed those directories,
R.java
started autogenerating for me again.通常这是因为您在创建项目时提供的 MinSDK 版本号。 示例:
如果您希望最低版本为2.1,那么Android 2.1实际上是API Level 7。
当您浏览您下载并安装的SDK时,您可以看到我在说什么。 导航到您安装 SDK 的位置(例如 C:\android-sdk-windows)并打开名为“platforms”的文件夹。 您将看到类似“android-7”的内容被列为文件夹,如果您打开该文件,则有一个
source.properties
文件,当使用文本编辑器打开时,将显示相应的平台版本。创建项目时,必须选择“构建目标”API,该列表中名为“API 级别”的最后一列显示您在填充 MinSDK 设置时要查找的数字。
这可能是导致
R.java
文件未在“Project >”下创建的最常见错误之一。 根> 包名> R.java。Often times this is because of the MinSDK version number you supplied when creating the project. Example:
If you want 2.1 to be the minimum, Android 2.1 is actually API Level 7.
You can see what I am talking about when you browse the SDK you downloaded and installed. Navigate to the place you installed the SDK to (
C:\android-sdk-windows
for example) and open the folder named "platforms". You will see something like "android-7" listed as a folder, and if you open that there is asource.properties
file that, when opened with a text editor, will show you the corresponding platform version.When you create a project, and you must select a "Build Target" API, the last column in that list named "API Level" shows the number you are looking for when populating the MinSDK setting.
This is probably one of the most common mistakes that results in the
R.java
file not being created under Project > gen > packagename > R.java.删除
main.out.xml
。 我对此很陌生,还不知道该文件的用途,但删除它可以解决问题。Remove
main.out.xml
. I'm new to this and don't yet know what this file is used for, but removing it cleared the problem.只需转到 Android 顶部菜单列表即可。 单击构建菜单,在“构建”下单击重建项目。
Just go to Android Top menu list. click on Build Menu, in under Build click on Rebuild Project.
首先检查xml布局是否有错误,如果有则先解决。
否则从项目中删除 junit 依赖项并重建项目。
First check is there any error in any xml layout or not, if then resolve it first.
Otherwise remove junit dependency from project and rebuild the project.
如果有人感兴趣(我可能会在这里拯救你的生命),我遇到了错误,
R.xml
无法解析,在 GLS 项目上略有不同。 嗯嗯。 查看R.java
后,我发现自动生成的类XML.java
(我认为)不存在。解决方案? 它需要在
res
中创建一个新文件夹:res\xml
和一个名为default_values.xml
的文件在那里。 然后一切就OK了。
以防万一您没有获得该文件,它是:
In case anyone is interested (I might be saving your life here), I had the error,
R.xml
cannot be resolved, slightly different on a GLS project. Hmmmm. After looking inR.java
, I found an auto-generated class,XML.java
, (I think) was not there.Solution? It needed a new folder in
res
:res\xml
and a file calleddefault_values.xml
in there. Then all was OK.
Just in case you have not got that file, it's:
所以我在切换构建目标时多次遇到这个问题。 平时做一个Project>> 清洁对我有用。 然而,这最后一次却没有。 最后,我尝试打开位于根项目文件夹下的 default.properties 文件。 我收到一条错误消息,指出它与文件系统不同步。 我实际上删除了它并复制了一个同事版本,该版本允许 eclipse 重建我的 R 文件。 我将在下面粘贴它的样子。 它被命名为“default.properties”。
So I have run into this problem multiple times when switching build targets. Usually doing a Project >> Clean worked for me. This last time, however, it did not. Finally I tried to open my default.properties file, located under the root project folder. I received an error message stating that it was out of sync with the file system. I actually deleted it and copied a coworkers version which allowed eclipse to rebuild my R file. I will paste what it looks like below. It is named 'default.properties'.
我有 Android 8 的示例,并尝试使用 Android 7 SDK。 当我关闭项目并重新打开应用程序文件夹并选择使用Android 8 SDK时,它能够找到R文件。 希望这可以帮助。
I had the examples of Android 8 and was trying to use Android 7 SDK. When I closed the project and reopened the application folder and chose to use Android 8 SDK, it was able to find the R file. Hope this helps.