我刚刚下载并安装了新的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不能被分解

在网上

setContentView(R.layout.main);

Why?

PS:我在res/layout/下有一个名为main.xml的XML文件。


当前回答

关闭所有文件,清理项目,重新启动Eclipse。

or

R是一个自动生成的类,它包含用于标识资源的常量。如果你没有R.java文件(在Eclipse的1.5 SDK中是gen/eu.mauriziopz.gps/R.java),我建议关闭并重新打开你的项目,或者转到project > Build all(并选择“自动构建”,而Josef则推荐)。如果这行不通,那就试着做一个新项目

其他回答

我认为问题出在layout.xml文件或import语句中。

请确保您没有导入R.android。,而不是导入R.YOUR PACKAGE NAME..

它会工作得很好。

还有一件事可能会导致这个问题:

我安装了新的ADT(第22节)。它停止创建包含R.java的gen文件夹。解决方案是在Android SDK管理器中安装新的Android SDK构建工具。

在这里找到解决方案

此外,如果你的res文件夹中有额外的文件,如“desktop.ini”,这些文件是操作系统添加的,但完全没有必要,试着删除它们。这显然是我导入项目中阻碍生成R.java的原因之一。

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

# This file is automatically generated by Android Tools.
# Do not modify this file -- YOUR CHANGES WILL BE ERASED!
# 
# This file must be checked in Version Control Systems.
# 
# To customize properties used by the Ant build system use,
# "build.properties", and override values to adapt the script to your
# project structure.

# Indicates whether an apk should be generated for each density.
split.density=false
# Project target.
target=android-3

这是一个很老的帖子,我今天有一个新发现。我已经创建了一个活动,我把它放在另一个包(错误)。在新创建的类中,eclipse无法解析R。

我尝试了很多地方提到的所有方法,但我没有注意到我把它放在了不同的包装里。挣扎了几分钟后,我发现我放错包装了。

如果您没有将Activity保存在Activity包中,则会得到此错误。