我刚刚下载并安装了新的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文件中的android:versionName从1.0更改为1.1。这重新生成了我的R.java文件,我很高兴。我希望这能帮助到一些人。

其他回答

我的项目有一个r.java.在开始,R.layout。主要工作好。但是,在添加了一些代码后,它不能工作,错误是r.b ayout.main不能解决。有什么问题吗?

查看您的导入。很有可能这句话是:

import android.R;

会在那里。如果是这种情况,删除它,这样你的项目就不会用默认的Android资源类来解析R,而是用从你的/res/文件夹自动生成的类来解析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.

最近,我遇到了类似的R无法解决的情况。

项目->清洁(它通常工作,但这次没有)。 我试图删除生成/文件夹(自动生成)-没有。 虚拟修改文件。不了。现在我头发已经变白了! 我在AndroidManifest.xml文件中检查appName是否存在。它被检查过了! 再次查看我的res/文件夹…

这就是奇怪的地方…它给出了一个错误,在某些情况下,但不是所有情况下。

我添加了格式化="false"标志:

<resources>
    <string name="blablah" formatted="false">
    </string>
</resources>

宾果!

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

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