我刚刚下载并安装了新的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 not found问题。

Right Click on the project
Click on Android Tools
Click on Rename Application Project
Enter the new package name

然后将出现一个弹出窗口,要求应用更改,包括R工作区。

在android中重命名包名

其他回答

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

这个错误突然出现在我的x64 Linux Mint安装中。结果是ADB二进制文件失败,因为没有安装ia32-libs包。只需运行apt-get install ia32-libs并重新启动Eclipse即可修复该错误。

如果你的x64发行版没有ia32-libs,你将不得不使用Multiarch。

检查这篇文章的#4和#5: http://crunchbang.org/forums/viewtopic.php?pid=277883#p277883

希望这能帮助到一些人。

重启你的项目

文件>>invalidate缓存/重启

注意:

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

要解决此问题,请转到java构建路径->order和Export,选中所有复选框,选择ok,然后清理并构建项目,导入包名R文件(Ex com.demo.R)。它的工作原理。

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.