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


当前回答

不应该尝试导入android.R。尝试修复XML文件中出现的所有错误,包括清单文件。当所有错误被清除后,R.java将重新出现。

其他回答

对我来说,这是因为错误的java编译器版本。 项目属性-> 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

我有这个问题很多次,我发现有时“R不会解决”,因为:

一些坏的资源已经存在,例如:一个名为My image .png的图像 或者在资源中出现一些错误,比如在xml文件中出现错误,或者在你的表单中定义资源时出现错误。

在这种情况下,你甚至不能将你自己的包R导入到你的项目中。所以去你的资源文件夹,搜索以上两点。

这是可行的

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

确保所有资源文件都没有大写扩展名。我发现我保存了一些带有大写扩展名。png的图像。我把它改成。png,一切都恢复正常了。