我刚刚下载并安装了新的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.java。 如果它是空的,可能你没有权限创建新文件,所以退出eclipse,然后右键单击,以管理员身份运行,清理项目,一切都会正确。

否则你的XML文件中可能会有错误,或者检查其他答案。

其他回答

在我的例子中,我试图将我的项目转换为Maven。过了一会儿(和成千上万的随机错误说什么),我试图撤销所有的操作。我没有注意到的是.project文件被更改了,并且在Eclipse中不可见。

只有将.project文件恢复到maven之前的版本才能帮助我修复这个错误。

不幸的是,现有的问题没有一个有用。由于我的问题似乎是平台特定的,这可能只适用于如果你是Ubuntu 64位(我目前使用Ubuntu 12)。 一开始我没有看到“问题”窗口,它已经暗示了一个解决方案,在64位环境中,你需要32位库,这可能会丢失:

Sudo apt-get安装ia32-libs

我安装了软件包并重新构建了我的项目,这为我解决了这个问题。

如果有人感兴趣(我可能会在这里挽救您的生命),我有一个错误,R.xml无法解析,在GLS项目上略有不同。嗯。在R.java中查找后,我发现一个自动生成的类,XML.java,(我想)不在那里。

解决方案吗?它需要在res: res\xml中创建一个新文件夹和一个名为default_values.xml的文件 在那里。然后一切都好了。

以防你没有那个文件,它是:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
    xmlns:android="http://schemas.android.com/apk/res/android">
</PreferenceScreen>

R.java是Android Eclipse插件创建的文件 构建应用程序。R.java是在“gen”下创建的 目录中。此文件由“res”中的信息生成。 目录中。如果在Eclipse上运行select“Project”->“Clean… 菜单,它将删除,然后重新生成R.java文件。

“R不能被解决”的问题发生在你改变你的 在AndroidManifest.xml文件中的包名。它使用安卓系统 包名下创建子目录“gen”目录,其中 它存储R.java文件。

Eclipse may have problems executing clean, because it is confused about where the R.java file is when you have changed the Android package name. You can either rename the subdirectory under gen to match your new package name, or you can change your package name back to the old name. Do the clean and then change the package name to the new name you want. This works best if you stop Eclipse from trying to build while you are changing the package name. Under the "Project" menu uncheck the option to "Build Automatically" and also when the "Clean..." dialog asks if it should "Start a build immediately" uncheck the box so it doesn't try to build while you are changing the package name. After you have changed the name you can turn "Build Automatically" back on again.

Note that if your AndroidManifest.xml file package name does not match your Java package name, Eclipse will end up automatically adding an "import <your Android package name>.R;" line in all your .java files that have any references to R. If you change your AndroidManifest.xml package name, sometimes Eclipse does not update all of these added imports. If that happens, use the Eclipse refactoring (ALT + Shift + R) to change the import statement in one of your Java files to your new AndroidManifest.xml package name. It is best to do this while you have disabled "Build Automatically".

如果您使用的是Mac,请检查. ds_store是否已添加到/res目录中(终端命令行中的ls -al)。删除它并刷新/重建。