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

其他回答

R不能解决的原因有几个。

大多数情况下,此错误发生在XML文件错误。这个错误可以通过Clean和重建你的项目来解决,也可以重新启动你的android studio。 有时候如果你在AndroidManifest中编码一些错误的东西,这个错误可能会发生,所以请检查这个。 如果资源包含相同的名称或包含无效字符,则会发生此错误。

如果你已经检查了这些,但你仍然面临着同样的问题,那么继续:


当Android Studio无法正确生成R.java文件时,就会出现这个错误,所以你必须遵循以下步骤:

第一步:从app目录中安全删除build文件夹。 步骤2:在Android Studio中,File -> Invalidate cache / Restart 步骤3:构建->清洁项目然后构建->重建项目。

所以构建文件夹将重新生成,希望错误已经消失。


如果仍然发现Error,那么可能您之前已经执行了包名重构。在这种情况下,您可以按ALT + ENTER一起导入R。

我将AndroidManifest.xml文件中的android:versionName从1.0更改为1.1。这重新生成了我的R.java文件,我很高兴。我希望这能帮助到一些人。

So I have run into this problem multiple times when switching build targets. Usually doing a Project >> Clean worked for me. This last time, however, it did not. Finally I tried to open my default.properties file, located under the root project folder. I received an error message stating that it was out of sync with the file system. I actually deleted it and copied a coworkers version which allowed eclipse to rebuild my R file. I will paste what it looks like below. It is named 'default.properties'.

# This file is automatically generated by Android Tools.
# Do not modify this file -- YOUR CHANGES WILL BE ERASED!
# 
# This file must be checked in Version Control Systems.
# 
# To customize properties used by the Ant build system use,
# "build.properties", and override values to adapt the script to your
# project structure.

# Indicates whether an apk should be generated for each density.
split.density=false
# Project target.
target=android-3

对我来说,我删除了“import android.R;”,它解决了我所有的问题。真是个噩梦!!

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

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

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