我刚刚下载并安装了新的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不能被分解

然后检查/res目录,一定有一些文件有一些错误,这是阻止应用程序被构建。例如,它可能是一个布局文件,也可能是由于某些缺少的资源,但您已经在XML文件中定义了它。

如果您在res/ drawabiles -mdpi这样的文件夹中有任何附加的,甚至是未使用的(!)或未引用的(!)图像,这些图像不符合文件命名约定(可能只包含[a-z0-9_.]), R.java类可能不会生成,导致所有其他帖子引用的事件链。

其他回答

我通过将资源文件直接放在Android指定的目录中解决了这个问题。我错误地认为R.raw可以包含我的资源的整个文件目录。所有文件都必须将R.raw作为直接父目录。

一些答案建议重新启动计算机。我发现重启adb服务器就足够了——如果你刚刚更新了Android SDK,这一点尤其正确。

通过linux shell重启adb:

$ cd $ANDROID_SDK/platform-tools
$ ./adb kill-server
$ ./adb start-server

您可能需要重新启动Eclipse,以便重新连接到重新启动的adb服务器。如果Eclipse没有自动编译项目,您可能需要执行一个clean/build。

您可能需要更新SDK工具。重新启动Android SDK管理器,并安装一个新项目:Android SDK Build-tools。

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

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

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

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