我刚刚下载并安装了新的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在android项目中是自动生成的资源,有时会发生这种情况,不知道为什么,但你试着从项目中自动生成选项

删除其他项目或android.r文件的其他导入 =>总是记住,如果你总是面临自动生成id或自动删除R文件的问题,你可以生成静态id,如字符串文件,并使用@id而不是@+id

其他回答

首先检查是否有任何错误在任何xml布局,如果然后解决它首先。

否则,从项目中删除junit依赖项并重新构建项目。

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.

按Alt+enter并导入

or

Ctrl + F9

和解决。

重启你的项目

文件>>invalidate缓存/重启

注意:

Android会自动创建一个不需要导入的。r文件 进口android.R。* << R.java文件由android Studio自动创建 Android工作室有很多缓存,这就是为什么有时会发生这种情况

哇。我通过在所有XML文件和相关资源中跟踪所有可能的错误来解决R问题。我知道这是一些层次,但简单地说,你必须:

确保所有引用的资源映像都在那里 大家都已经提到这么多了,这只是回声…如果需要,删除所有Eclipse注入的R.*导入。 在执行上述操作时,我喜欢重新启动Eclipse。 确保所有XML文件的结构正确。就像,做你的事情。哈哈哈 尽量多写几行。养成经常按Ctrl + Shift + S的习惯,可以快速改变编译器的结果。

Hahah。当然,得到R是很好的,但不能解决固定…