我刚刚下载并安装了新的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未生成甚至消失的问题时,这都是由于XML布局文件中的某些问题导致应用程序无法构建。

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

其他回答

I would say "R can not be resolved" is something too general and can be misleading at times. I also had the same problem many times ago even when I first create a new project (which mean I did not mess with the code yet). Most of the time I just need to update my sdk manager (as some people have mentioned). But in my recent case, I found out that the path to my project file is too long (something like this E:\R&D\ANDROID\ANDROID BASIC\Folder 1\ Folder 2\..... \Folder n\MyProject). The folder names also contain spaces character. I thought it could be the case and indeed it's true, when I created a new project in another folder with shortest path possible, and none of the folders' names containing space character, it worked as normal.

通常情况下,这是因为您在创建项目时提供的MinSDK版本号。例子:

如果你想把2.1作为最小值,Android 2.1实际上是API Level 7。

当你浏览下载并安装的SDK时,你就会明白我在说什么。导航到你安装SDK的地方(C:\android-sdk-windows为例),打开名为“平台”的文件夹。你会看到像“android-7”这样的东西被列为一个文件夹,如果你打开它,就会有一个源代码。属性文件,当用文本编辑器打开时,将显示相应的平台版本。

当你创建一个项目时,你必须选择一个“Build Target”API,该列表中名为“API Level”的最后一列显示了你在填充MinSDK设置时所寻找的数字。

这可能是导致R.java文件没有在Project > gen > packagename > R.java下创建的最常见错误之一。

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

我的问题是,我从strings.xml中的字符串中遗漏了=标记 例如:<string name"dialog_grl…" 即使在我的.xml文件的图形布局无法显示字符串时,我也没有发现它。在一次项目清理后,所有文件都被弄乱了。

每次我遇到这个错误,总是落在SO。 说到底,原因都是一样的。

所以请永远记住这张小纸条

我发现大部分时间的原因是-错误的XML布局

如果您的XML不正确,则R文件不会在 构建。