我刚刚下载并安装了新的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 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环境中,可以搞砸自己的方法确实有无数种。

将构建目标从1.5或1.6更改为2.2 (API版本8),并检查string.xml中是否有%字符。如果是,则替换为%%。

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

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

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

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

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

对我来说,这是因为错误的java编译器版本。 项目属性-> Java编译器,启用“启用项目特定设置”,并选择您的“编译器遵从级别”