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


当前回答

对我来说,这是因为错误的java编译器版本。 项目属性-> Java编译器,启用“启用项目特定设置”,并选择您的“编译器遵从级别”

其他回答

我的问题是,我从strings.xml中的字符串中遗漏了=标记 例如:<string name"dialog_grl…" 即使在我的.xml文件的图形布局无法显示字符串时,我也没有发现它。在一次项目清理后,所有文件都被弄乱了。

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

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

在这里找到解决方案

检查上面的类文件导入,确保没有android的导入。R,如果它有,然后删除它,然后按Ctrl + Shift + o,选择packagename。R不导入另一个。

我的R文件消失了,不会生成-控制台报告了这个:

[2010-09-22 16:19:40 - myproject] res\ draw- ldpi\downloadNow.png:无效的文件名:必须只包含[a-z0-9_.]

因此,资源名称不能有大写字母-确认它们都是小写字母并包含。而且只有_

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.