我刚刚下载并安装了新的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则推荐)。如果这行不通,那就试着做一个新项目

其他回答

如果您使用的是Mac,请检查. ds_store是否已添加到/res目录中(终端命令行中的ls -al)。删除它并刷新/重建。

重新启动计算机。 除了这种方法,什么也帮不了我

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包中,则会得到此错误。

当你得到

R不能被分解

然后检查/res目录,一定有一些文件有一些错误,这是阻止应用程序被构建。例如,它可能是一个布局文件,也可能是由于某些缺少的资源,但您已经在XML文件中定义了它。

如果您在res/ drawabiles -mdpi这样的文件夹中有任何附加的,甚至是未使用的(!)或未引用的(!)图像,这些图像不符合文件命名约定(可能只包含[a-z0-9_.]), R.java类可能不会生成,导致所有其他帖子引用的事件链。