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


当前回答

只是清理和重建你的项目有时会有帮助

确保在android中的res/drawable文件夹中没有错误。在命名此文件夹中的图像时,不要给出数字和大写字母。

其他回答

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

1)首先关闭所有文件。 2)进入Build ->清洁项目。 3)进入Build ->重建项目。

这对我很有用。

这对我很管用。

我没有“import android”。R’在eclipse向导创建的全新项目中。默认的XML布局文件也是小写的。

问题是eclipse没有生成R.java。再多的清理/重新启动也没有任何效果。

我注释了setContentView和getMenuInflator调用,make成功后,Eclipse生成了R.java。

一旦生成了R.java,我就可以取消setContentView和getMenuInflator的注释。

R有两种出错的方式

1)布局文件ie xml文件的问题

2)如果你更新adt,那么就会出现R问题。 -你必须检查项目的属性 - goto->属性->Java构建路径->命令和导出,并检查所有的lib。

这将解决问题。

当你刚刚打开你的项目时, android studio需要一段时间来加载东西。 这种事在我身上经常发生。我只是坐下来,看一个youtube视频5分钟,而它完成加载。

到那时,不能解析符号R已经消失了!:-)

如果你已经反复检查了上面提到的一切,确保项目已经建成,我建议你耐心等待……如果不清理并重建项目。