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


当前回答

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

其他回答

So I have run into this problem multiple times when switching build targets. Usually doing a Project >> Clean worked for me. This last time, however, it did not. Finally I tried to open my default.properties file, located under the root project folder. I received an error message stating that it was out of sync with the file system. I actually deleted it and copied a coworkers version which allowed eclipse to rebuild my R file. I will paste what it looks like below. It is named 'default.properties'.

# This file is automatically generated by Android Tools.
# Do not modify this file -- YOUR CHANGES WILL BE ERASED!
# 
# This file must be checked in Version Control Systems.
# 
# To customize properties used by the Ant build system use,
# "build.properties", and override values to adapt the script to your
# project structure.

# Indicates whether an apk should be generated for each density.
split.density=false
# Project target.
target=android-3

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

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

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

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

威尔说的对吗

R是一个自动生成的类,它包含用于标识>资源的常量。如果你没有R.java文件(它将是gen/eu.mauriziopz.gps/R.java在>Eclipse与1.5 SDK),我建议关闭和重新打开你的项目或去>项目>构建所有(并选择“自动构建”,而>Josef的建议)。如果这不起作用,那就尝试做一个新项目,如果问题是重新创建的,那么>帖子在这里再次发布,我们将更详细地介绍。

但我发现还有另一个问题导致了第一个问题。SDK目录中的工具没有被执行的权限,所以它就像Eclipse不存在一样,因此它没有构建R.java文件。

因此,修改权限并选择“自动构建”就解决了这个问题。

真正的问题是如何防止android.R;返回:

窗口-> Prefs -> Java ->编辑器->保存动作

取消勾选“组织导入”。

这样就行了。

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