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


当前回答

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

其他回答

有时,手动创建R.java文件会有帮助,只是简单的空R.java。

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.

一个更好更有前途的解决方案

关闭Eclipse并打开Workspace目录。 寻找一个名为"的文件夹。然后删除该文件夹。 打开Eclipse。

错误应该消失:)

R.java可能无法自动生成的另一个原因是,如果您有res/ drawablehdpi、res/ drawablemdpi或res/ drawableldpi这样的目录。

1.6+似乎对这些目录没有问题,但1.5不需要它们。当我删除这些目录时,R.java再次开始为我自动生成。

删除main.out.xml。我是新手,还不知道这个文件是用来做什么的,但删除它解决了这个问题。