我刚刚下载并安装了新的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文件会有帮助,只是简单的空R.java。

其他回答

如果清理项目不起作用,如果xml文件没有问题(如果它是一个新项目)。然后转到Androidmanifest.xml,编辑它并在行之间添加一些空格,保存它。现在清理项目,错误将消失。

这是因为陈旧的R.java文件。当我们编辑android manifest文件时,再次创建R.java。

如果有人感兴趣(我可能会在这里挽救您的生命),我有一个错误,R.xml无法解析,在GLS项目上略有不同。嗯。在R.java中查找后,我发现一个自动生成的类,XML.java,(我想)不在那里。

解决方案吗?它需要在res: res\xml中创建一个新文件夹和一个名为default_values.xml的文件 在那里。然后一切都好了。

以防你没有那个文件,它是:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
    xmlns:android="http://schemas.android.com/apk/res/android">
</PreferenceScreen>

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

I had tried all of the above with no prevail. It turned out PhoneGap was causing a conflict with the Android SDK. After uninstalling PhoneGap, the Resources file started regenerating again. Another project, needed PhoneGap so I re-installed, and once again, the Android project (a different project) stopped auto-generating the R file - Build Automatically was checked, did the Clean, and restarted Eclipse - no dice. I removed PhoneGap and it was working once again. This was PhoneGap v 1.5 with the MDS 1.1 plugin for Eclipse.

解决方案:

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