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


当前回答

我的解决方案(在Eclipse Juno上)是点击“Fix Project properties”,通过:

在项目上单击鼠标右键 选择Android工具菜单 单击修复项目属性

ps.首先检查R库是否未实现,res/文件夹中的所有文件是否正常。

其他回答

在我的情况下,我的AndroidManifest.xml有一个错误。其他人说你的XML文件必须没有错误,但我只在res/文件夹里看了看。发现并修复尽可能多的错误,问题就会自行解决。

我通过将资源文件直接放在Android指定的目录中解决了这个问题。我错误地认为R.raw可以包含我的资源的整个文件目录。所有文件都必须将R.raw作为直接父目录。

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.

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

我认为问题出在layout.xml文件或import语句中。

请确保您没有导入R.android。,而不是导入R.YOUR PACKAGE NAME..

它会工作得很好。