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


当前回答

重启你的项目

文件>>invalidate缓存/重启

注意:

Android会自动创建一个不需要导入的。r文件 进口android.R。* << R.java文件由android Studio自动创建 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.

检查XML文件名。确保它们都是小写的。

还要确保所有图像资源名称也都是小写的。我在我的jpg文件的名称中有一个大写字母,这在我的项目中导致了R未解决的错误。

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

用包名导入R类。不导入android R类。

您可能需要更新/安装SDK工具。重新启动Android SDK管理器,并安装一个新项目:Android SDK Build-tools。逐个删除,修正哪一个适合你。