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


当前回答

我猜你只是复制粘贴了密码,或者没有。无论哪种方法,检查AndroidManifest.xml文件,看看包名是否正确。

其他回答

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

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

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

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

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

R.java不能自动生成的原因是如果你有这样的目录

res/drawable-hdpi, 
res/drawable-mdpi, 
res/drawable-ldpi.

image name should be in a proper way

检查映像名称 并检查和布局/any.xml 和string.xml

在许多可能导致这种情况的问题中,这是为我解决的问题。

右键单击项目->属性-> Java构建路径->源。

然后添加gen和src文件夹。出于某种原因,Android希望我只保存整个文件夹。

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.