我刚刚下载并安装了新的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是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.在开始,R.layout。主要工作好。但是,在添加了一些代码后,它不能工作,错误是r.b ayout.main不能解决。有什么问题吗?

查看您的导入。很有可能这句话是:

import android.R;

会在那里。如果是这种情况,删除它,这样你的项目就不会用默认的Android资源类来解析R,而是用从你的/res/文件夹自动生成的类来解析R。

我有这个问题很多次,我发现有时“R不会解决”,因为:

一些坏的资源已经存在,例如:一个名为My image .png的图像 或者在资源中出现一些错误,比如在xml文件中出现错误,或者在你的表单中定义资源时出现错误。

在这种情况下,你甚至不能将你自己的包R导入到你的项目中。所以去你的资源文件夹,搜索以上两点。

这是可行的

在我的情况下,我的AndroidManifest.xml有一个错误。其他人说你的XML文件必须没有错误,但我只在res/文件夹里看了看。发现并修复尽可能多的错误,问题就会自行解决。

在运行x64 Linux时,您可能会丢失一些x32构建库。对于Debian,请确保安装了libc6-i386和zlib1g包。

当你得到

R不能被分解

然后检查/res目录,一定有一些文件有一些错误,这是阻止应用程序被构建。例如,它可能是一个布局文件,也可能是由于某些缺少的资源,但您已经在XML文件中定义了它。

如果您在res/ drawabiles -mdpi这样的文件夹中有任何附加的,甚至是未使用的(!)或未引用的(!)图像,这些图像不符合文件命名约定(可能只包含[a-z0-9_.]), R.java类可能不会生成,导致所有其他帖子引用的事件链。