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

or

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

对我来说有用的是安装“Android SDK Build-tools”。打开Android SDK管理器,安装Android SDK构建工具。它还帮助更新了Eclipse中的android开发平台:帮助>检查更新

尝试使您的新XML布局文件名小写。例如,使用my_file.xml而不是myFile.xml。

我的R文件消失了,不会生成-控制台报告了这个:

[2010-09-22 16:19:40 - myproject] res\ draw- ldpi\downloadNow.png:无效的文件名:必须只包含[a-z0-9_.]

因此,资源名称不能有大写字母-确认它们都是小写字母并包含。而且只有_

我遇到了同样的问题,但找不到哪个xml文件导致了错误。

多亏了这篇文章,我安装了18.1.1而不是19的构建工具,它显示了有缺陷的文件。看起来版本19并没有显示实际的错误。

希望它能帮助到一些人:-)