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


当前回答

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

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

在这里找到解决方案

其他回答

在追踪了这个问题之后,我在Android文档中发现了这样的说明:

http://source.android.com/source/using-eclipse.html

*注意:Eclipse有时喜欢添加一个“导入android”。R"的声明 使用资源的文件顶部, 特别是当您要求Eclipse这样做时 对导入进行排序或管理。这 会让你的成就破碎。看 为了这些错误的输入 语句并删除

在浏览Android示例教程时,我经常使用Ctrl + Shift + O命令来“组织导入”并生成任何缺失的导入语句。有时这将生成不正确的import语句,该语句将隐藏在构建时自动生成的R.java类。

发生这种情况的另一种方式是,从其中一个示例启动一个新项目。当您稍后决定将包名从com.example.android.foo更改为您自己的域时,您将需要修改清单和单个.java文件中的几个值。

如果您使用的是Eclipse,请找到.java文件的包语句并选择QuickFix。这里可能有几个选项,但你想要的是指示“移动‘foo.java’到打包‘com.youdomain.android.yourapp’”的选项。保存文件,它可以自动构建或做其他人建议,并尝试“项目->清洁”。

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.

值得在AndroidManifest.xml中检查一下。属性包的值正确。

那就是:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="your.correct.package.name"
   ...

更改后,将重新生成R.java。

解决方案:

Eclipse重新启动后项目干净。它们是自动生成的,否则你是XML内部的任何组件(如按钮)后按Ctrl + Shift + o。它自动生成R文件,然后错误就解决了。