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

其他回答

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

不应该尝试导入android.R。尝试修复XML文件中出现的所有错误,包括清单文件。当所有错误被清除后,R.java将重新出现。

用包名导入R类。不导入android R类。

每次我遇到这个错误,总是落在SO。 说到底,原因都是一样的。

所以请永远记住这张小纸条

我发现大部分时间的原因是-错误的XML布局

如果您的XML不正确,则R文件不会在 构建。

当你得到

R不能被分解

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

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