我刚刚下载并安装了新的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不能自动生成的原因是如果你有这样的目录

res/drawable-hdpi, 
res/drawable-mdpi, 
res/drawable-ldpi.

image name should be in a proper way

检查映像名称 并检查和布局/any.xml 和string.xml

其他回答

检查你导入到应用程序中的库项目是否都是打开的。

出于某种原因,如果你没有向活动添加任何东西,比如onCreate(),它就不会理解R是什么。

在我的情况下,这个错误是因为我导入了一个图像到文件夹/res,图像有一个无效的名称。真不敢相信,我花了将近一个小时来解决这个问题,直到我意识到图像有一个无效的名称:/

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

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

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

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

同样的事情也发生在我身上,我花了一天时间来解决这个问题。这真的是一个愚蠢的错误。我只是剪一下我的经验,希望对您有所帮助。首先,当你输入这个setContentView(r.b ayout.activity_to_do_list);听到你的起点R.l,那么eclipse应该为你打开一个这样的建议

你导入第二个不是layout-android。R。第二个是在项目中创建的,其中com.example.gridtest是项目包的名称。然后在代码中集中导入的部分

好的,看这个导入com.example。gridtest。r;这很重要。如果你已经导入了这个android。R然后移除它。谢谢,希望有用。(你不需要总是这样做,但如果你遇到这种问题,然后这样做,希望它会有所帮助)