我刚刚下载并安装了新的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文件。


当前回答

I would say "R can not be resolved" is something too general and can be misleading at times. I also had the same problem many times ago even when I first create a new project (which mean I did not mess with the code yet). Most of the time I just need to update my sdk manager (as some people have mentioned). But in my recent case, I found out that the path to my project file is too long (something like this E:\R&D\ANDROID\ANDROID BASIC\Folder 1\ Folder 2\..... \Folder n\MyProject). The folder names also contain spaces character. I thought it could be the case and indeed it's true, when I created a new project in another folder with shortest path possible, and none of the folders' names containing space character, it worked as normal.

其他回答

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

首先查看你的生成文件,看它是否包含你的包名和R.java。 如果它是空的,可能你没有权限创建新文件,所以退出eclipse,然后右键单击,以管理员身份运行,清理项目,一切都会正确。

否则你的XML文件中可能会有错误,或者检查其他答案。

我今天也遇到了同样的问题。我尝试使用sdk管理器执行所有sdk更新。完成并重新启动IDE后,问题已解决。

将构建目标从1.5或1.6更改为2.2 (API版本8),并检查string.xml中是否有%字符。如果是,则替换为%%。

在将一些图形放入可绘制文件夹后,我也遇到了同样的问题。有一些错误,当我试图清理,R.java文件不存在。

所以我仔细检查了一下,发现有一些文件的文件名是大写的,有一些文件的文件名是相同的,但是文件扩展名不同。我重命名了这些文件,每个文件都有一个小写的唯一名称。记住,Android关心的是文件名,而不是扩展名。

也许这对你或其他读者有帮助。