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

所以我仔细检查了一下,发现有一些文件的文件名是大写的,有一些文件的文件名是相同的,但是文件扩展名不同。我重命名了这些文件,每个文件都有一个小写的唯一名称。记住,Android关心的是文件名,而不是扩展名。

也许这对你或其他读者有帮助。

其他回答

检查你导入到应用程序中的库项目是否都是打开的。

出于某种原因,如果你没有向活动添加任何东西,比如onCreate(),它就不会理解R是什么。

解决方案:

Eclipse重新启动后项目干净。它们是自动生成的,否则你是XML内部的任何组件(如按钮)后按Ctrl + Shift + o。它自动生成R文件,然后错误就解决了。

将活动添加到与包的根名称空间不同的名称空间也可能导致此错误。

例如,如果com.example.myapp是包的根名称空间,那么您可以将一个活动添加到com.example.myapp.activities名称空间。

这将产生“R不能被解决”的错误。

要修复导入,活动中默认命名空间中的R应该是:

import com.example.myapp.R;

R是一个生成的类。如果你正在使用Android开发工具(ADT),它会在项目构建时生成。您可能已经关闭了“自动构建”。

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