R 无法解析 - Android 错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/885009/
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
R cannot be resolved - Android error
提问by Maurizio Pozzobon
I just downloaded and installed the new Android SDK. I wanted to create a simple application to test drive it.
我刚刚下载并安装了新的 Android SDK。我想创建一个简单的应用程序来测试驱动它。
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 Eclipsegives me the error
但Eclipse给了我错误
R cannot be resolved
R无法解析
on line
在线的
setContentView(R.layout.main);
Why?
为什么?
PS: I do have an XML file named main.xml
under res/layout/
.
PS:我确实有一个名为main.xml
.xml的 XML 文件res/layout/
。
采纳答案by Maurizio Pozzobon
What Will said was right
威尔说的是对的
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.
R 是一个自动生成的类,它保存用于标识您的 > 资源的常量。如果你没有 R.java 文件(它是 gen/eu.mauriziopz.gps/R.java in >Eclipse with the 1.5 SDK)我建议关闭并重新打开你的项目或去 >Project > Build all (并根据>Josef 的建议选择“自动构建”)。如果这不起作用,请尝试创建一个新项目,如果问题再次出现,请再次 > 在此处发布,我们将详细介绍。
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.
但我发现还有另一个问题导致了第一个问题。SDK目录下的tools没有执行权限,Eclipse好像不存在,所以没有构建R.java
文件。
So modifying the permission and selecting "Build Automatically" solved the problem.
因此修改权限并选择“自动构建”解决了问题。
回答by Michael Levy
After tracking down this problem as well, I found this note in the Android documentation:
在追踪到这个问题之后,我在 Android 文档中发现了这个注释:
http://source.android.com/source/using-eclipse.html
http://source.android.com/source/using-eclipse.html
*Note: Eclipse sometimes likes to add an "import android.R" statement at the top of your files that use resources, especially when you ask Eclipse to sort or otherwise manage imports. This will cause your maketo break. Look out for these erroneous import statements and delete them.*
*注意:Eclipse 有时喜欢在使用资源的文件顶部添加“import android.R”语句,尤其是当您要求 Eclipse 排序或以其他方式管理导入时。这将导致您的make中断。注意这些错误的导入语句并删除它们。*
While going through the Android sample tutorials, I would often 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.java
class that is automatically generated when you build.
在浏览 Android 示例教程时,我经常使用Ctrl+ Shift+O命令来“组织导入”并生成任何缺少的导入语句。有时这会生成不正确的导入语句,它会隐藏R.java
构建时自动生成的类。
回答by Luc
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.
每次我遇到 R 没有生成甚至消失的问题时,这是由于 XML 布局文件中的某些问题阻止了应用程序的构建。
回答by Vasu
Whenever you get
每当你得到
R cannot be resolved
R无法解析
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.
然后检查/res
目录,并且必须有一些文件中有一些错误并且阻止了应用程序的构建。例如,它可能是一个布局文件,也可能是由于缺少某些资源,但您已经在 XML 文件中定义了它。
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. Hope it helps!
如果您在 res/drawables-mdpi 等文件夹中有任何额外的、甚至未使用 (!) 或未引用 (!) 的图像,这些图像不符合文件命名约定(可能仅包含 [a-z0-9_.]),则R.java 类可能不会生成,导致所有其他帖子引用的事件链。希望能帮助到你!
回答by Dimitar Dimitrov
my project have include a r.java.at the beginning ,R.layout.main work good.But,after adding some code it doesn't work,and the error is R.layout.main can't resolved.what's the problem?
我的项目在开始时包含一个 r.java. ,R.layout.main 工作良好。但是,添加一些代码后它不起作用,错误是 R.layout.main 无法解决。是什么问题?
Look at your imports. Chances are that the line:
看看你的进口。可能是该行:
import android.R;
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.
将在那里。如果是这种情况,请将其删除,这样您的项目将不使用默认的 Android Resources 类解析 R,而是使用从 /res/ 文件夹自动生成的类。
回答by Micha? K
And another thing which may cause this problem:
还有另一件事可能会导致这个问题:
I installed the new ADT (v. 22). It stopped creating gen
folder which includes R.java
. The solution was to also install new Android SDK Build Tools from Android SDK Manager.
我安装了新的 ADT (v. 22)。它停止创建gen
包含R.java
. 解决方案是还从 Android SDK 管理器安装新的 Android SDK 构建工具。
Solution found here
解决方案在这里找到
回答by Micha? K
R.java
is a file that the Android Eclipse plugins creates while
building 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.
R.java
是 Android Eclipse 插件在构建应用程序时创建的文件。R.java
在“gen”目录下创建。该文件是根据“res”目录中的信息生成的。如果您在 Eclipse 菜单上运行选择“项目”->“清理...”,它将删除然后重新生成R.java
文件。
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.
当您更改AndroidManifest.xml
文件中的包名称时,会出现“R 无法解析”的问题。它使用您的 Android 包名称在“gen”目录下创建一个子目录,用于存储R.java
文件。
Eclipse may have problems executing clean, because it is confused about
where the R.java
file is when you have changed the Android package
name. You can either rename the subdirectory under gen
to match your
new 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.
Eclipse 在执行clean 时可能会出现问题,因为R.java
当您更改Android 包名称时,它对文件的位置感到困惑。您可以重命名下的子目录gen
以匹配新的包名称,也可以将包名称改回旧名称。进行清理,然后将包名称更改为您想要的新名称。如果您在更改包名称时阻止 Eclipse 尝试构建,这将最有效。在“项目”菜单下取消选中“自动构建”选项,并且当“清理...”对话框询问是否应该“立即开始构建”时取消选中该框,这样它就不会在您更改时尝试构建包名。更改名称后,您可以再次打开“自动构建”。
Note that if your AndroidManifest.xml
file package name does not match
your Java package name, Eclipse will end up automatically adding an
"import <your Android package name>.R;" line in all your .java
files
that 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 this
while you have disabled "Build Automatically".
请注意,如果您的AndroidManifest.xml
文件包名称与您的 Java 包名称不匹配,Eclipse 最终会自动添加一个“import <your Android package name>.R;” 在所有.java
引用 R 的文件中添加一行。如果更改AndroidManifest.xml
包名称,有时 Eclipse 不会更新所有这些添加的导入。如果出现这种情况,使用Eclipse的重构(ALT+
Shift+ R)在Java文件中的一个来改变import语句到新AndroidManifest.xml
包的名字。最好在禁用“自动构建”时执行此操作。
回答by Will
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.
R 是一个自动生成的类,其中包含用于标识资源的常量。如果您没有 R.java 文件(在带有 1.5 SDK 的 Eclipse 中它将是 gen/eu.mauriziopz.gps/R.java),我建议您关闭并重新打开您的项目或转到 Project > Build all(和在那里按照约瑟夫的建议选择“自动构建”)。如果这不起作用,请尝试创建一个新项目,如果问题再次出现,请再次在此处发布,我们将详细介绍。
回答by sharika
Close all files, clean project, restart Eclipse.
关闭所有文件,清理项目,重新启动 Eclipse。
回答by green
It is worth checking in AndroidManifest.xml
. The attribute package
has the correct value.
值得入住AndroidManifest.xml
。该属性package
具有正确的值。
That is:
那是:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="your.correct.package.name"
...
After you change that, the R.java
will be re-generated.
更改后,R.java
将重新生成。