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


当前回答

检查XML文件的内容。如果layout_width或layout_height包含match_parent元素,则将其更改为fill_parent。这为我解决了问题。

其他回答

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

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

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

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

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.

哇。我通过在所有XML文件和相关资源中跟踪所有可能的错误来解决R问题。我知道这是一些层次,但简单地说,你必须:

确保所有引用的资源映像都在那里 大家都已经提到这么多了,这只是回声…如果需要,删除所有Eclipse注入的R.*导入。 在执行上述操作时,我喜欢重新启动Eclipse。 确保所有XML文件的结构正确。就像,做你的事情。哈哈哈 尽量多写几行。养成经常按Ctrl + Shift + S的习惯,可以快速改变编译器的结果。

Hahah。当然,得到R是很好的,但不能解决固定…

可能发生这种情况的另一个原因是:今晚,我遇到了可怕的错误,最终发现一个错误的鼠标拖动显然将我自己的包的一个类移动到了另一个包的层次结构中。在Eclipse环境中,可以搞砸自己的方法确实有无数种。

这可能是由于手动重命名AndroidManifest.xml中的包而导致的问题

另一种选择是使用内置工具重命名包,这将照顾到大多数问题,避免R not found问题。

Right Click on the project
Click on Android Tools
Click on Rename Application Project
Enter the new package name

然后将出现一个弹出窗口,要求应用更改,包括R工作区。

在android中重命名包名