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


当前回答

最近,我遇到了类似的R无法解决的情况。

项目->清洁(它通常工作,但这次没有)。 我试图删除生成/文件夹(自动生成)-没有。 虚拟修改文件。不了。现在我头发已经变白了! 我在AndroidManifest.xml文件中检查appName是否存在。它被检查过了! 再次查看我的res/文件夹…

这就是奇怪的地方…它给出了一个错误,在某些情况下,但不是所有情况下。

我添加了格式化="false"标志:

<resources>
    <string name="blablah" formatted="false">
    </string>
</resources>

宾果!

其他回答

只是清理和重建你的项目有时会有帮助

确保在android中的res/drawable文件夹中没有错误。在命名此文件夹中的图像时,不要给出数字和大写字母。

每次我遇到R未生成甚至消失的问题时,这都是由于XML布局文件中的某些问题导致应用程序无法构建。

R是一个生成的类。如果你正在使用Android开发工具(ADT),它会在项目构建时生成。您可能已经关闭了“自动构建”。

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.

这是一个很老的帖子,我今天有一个新发现。我已经创建了一个活动,我把它放在另一个包(错误)。在新创建的类中,eclipse无法解析R。

我尝试了很多地方提到的所有方法,但我没有注意到我把它放在了不同的包装里。挣扎了几分钟后,我发现我放错包装了。

如果您没有将Activity保存在Activity包中,则会得到此错误。

在运行x64 Linux时,您可能会丢失一些x32构建库。对于Debian,请确保安装了libc6-i386和zlib1g包。