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


当前回答

还有一件事可能会导致这个问题:

我安装了新的ADT(第22节)。它停止创建包含R.java的gen文件夹。解决方案是在Android SDK管理器中安装新的Android SDK构建工具。

在这里找到解决方案

其他回答

这个错误突然出现在我的x64 Linux Mint安装中。结果是ADB二进制文件失败,因为没有安装ia32-libs包。只需运行apt-get install ia32-libs并重新启动Eclipse即可修复该错误。

如果你的x64发行版没有ia32-libs,你将不得不使用Multiarch。

检查这篇文章的#4和#5: http://crunchbang.org/forums/viewtopic.php?pid=277883#p277883

希望这能帮助到一些人。

对我来说,这是因为使用了“私人”android drawables:

[2015-11-26 18:59:48 - MyAndroidApp] 
/Users/myname/git/MyAndroidApp/res/menu/drawer_view.xml:24: 
error: Error: Resource is not public. (at 'icon' with value 
'@android:drawable/ic_menu_home').

有趣的是,直到我使用图标删除了对文件的引用,这个错误才出现在控制台中。

android:entries="@menu/drawer_view"

现在解决方案非常明显,将图标复制到我的项目(res/draw - xxxx /)并切换到本地引用。

@drawable/ic_menu_home

希望这能有所帮助。

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

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".

在将一些图形放入可绘制文件夹后,我也遇到了同样的问题。有一些错误,当我试图清理,R.java文件不存在。

所以我仔细检查了一下,发现有一些文件的文件名是大写的,有一些文件的文件名是相同的,但是文件扩展名不同。我重命名了这些文件,每个文件都有一个小写的唯一名称。记住,Android关心的是文件名,而不是扩展名。

也许这对你或其他读者有帮助。