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


当前回答

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

其他回答

我也有这个问题。结果发现我无意中从strings.xml文件中删除了“app_name”字符串资源,这导致了一个无声错误。一旦我重新添加了它,R类就成功生成了,一切都恢复并运行了。

要解决此问题,请转到java构建路径->order和Export,选中所有复选框,选择ok,然后清理并构建项目,导入包名R文件(Ex com.demo.R)。它的工作原理。

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文件消失了,不会生成-控制台报告了这个:

[2010-09-22 16:19:40 - myproject] res\ draw- ldpi\downloadNow.png:无效的文件名:必须只包含[a-z0-9_.]

因此,资源名称不能有大写字母-确认它们都是小写字母并包含。而且只有_

威尔说的对吗

R是一个自动生成的类,它包含用于标识>资源的常量。如果你没有R.java文件(它将是gen/eu.mauriziopz.gps/R.java在>Eclipse与1.5 SDK),我建议关闭和重新打开你的项目或去>项目>构建所有(并选择“自动构建”,而>Josef的建议)。如果这不起作用,那就尝试做一个新项目,如果问题是重新创建的,那么>帖子在这里再次发布,我们将更详细地介绍。

但我发现还有另一个问题导致了第一个问题。SDK目录中的工具没有被执行的权限,所以它就像Eclipse不存在一样,因此它没有构建R.java文件。

因此,修改权限并选择“自动构建”就解决了这个问题。