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


当前回答

1)首先关闭所有文件。 2)进入Build ->清洁项目。 3)进入Build ->重建项目。

这对我很有用。

其他回答

I had tried all of the above with no prevail. It turned out PhoneGap was causing a conflict with the Android SDK. After uninstalling PhoneGap, the Resources file started regenerating again. Another project, needed PhoneGap so I re-installed, and once again, the Android project (a different project) stopped auto-generating the R file - Build Automatically was checked, did the Clean, and restarted Eclipse - no dice. I removed PhoneGap and it was working once again. This was PhoneGap v 1.5 with the MDS 1.1 plugin for Eclipse.

右键单击项目,单击刷新或按F5

R是一个自动生成的类,它包含用于标识资源的常量。如果你没有R.java文件(在Eclipse的1.5 SDK中是gen/eu.mauriziopz.gps/R.java),我建议关闭并重新打开你的项目,或者转到project > Build all(并选择“自动构建”,而Josef则推荐)。如果这不起作用,那就尝试做一个新的项目,如果问题被重新创建,那就在这里再次发布,我们将更详细地讨论。

我遇到了同样的问题,但找不到哪个xml文件导致了错误。

多亏了这篇文章,我安装了18.1.1而不是19的构建工具,它显示了有缺陷的文件。看起来版本19并没有显示实际的错误。

希望它能帮助到一些人:-)

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