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


当前回答

我尝试了这个帖子上的大部分选项(以及其他许多选项),但没有一个对我有效。最后这个成功了,所以我想和大家分享一下……

我的机器上有多个版本的Eclipse IDE(因为我也从事与Android无关的项目)。我偶然地使用另一个Eclipse版本(非Android)使用的工作区创建了我的Android项目。由于这个原因,我猜有些奇怪的事情正在发生,我的R.java从未生成!

为了解决这个问题,我所做的就是创建一个新的工作空间,神奇地R.java开始生成,一切都很顺利……

所以,当你尝试所有其他选择时,也试试这个。希望这对你有用……

其他回答

此外,如果你的res文件夹中有额外的文件,如“desktop.ini”,这些文件是操作系统添加的,但完全没有必要,试着删除它们。这显然是我导入项目中阻碍生成R.java的原因之一。

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

R是一个自动生成的类,它包含用于标识资源的常量。如果你没有R.java文件(在Eclipse的1.5 SDK中是gen/eu.mauriziopz.gps/R.java),我建议关闭并重新打开你的项目,或者转到project > Build all(并选择“自动构建”,而Josef则推荐)。如果这不起作用,那就尝试做一个新的项目,如果问题被重新创建,那就在这里再次发布,我们将更详细地讨论。

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

检查XML文件名。确保它们都是小写的。

还要确保所有图像资源名称也都是小写的。我在我的jpg文件的名称中有一个大写字母,这在我的项目中导致了R未解决的错误。