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


当前回答

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.

其他回答

它为我工作与右击>Android工具>修复项目属性和重新启动Eclipse后的一些时间。

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

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

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

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

import com.example.myapp.R;

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

我也有同样的问题。在我的情况下,必须与布局XML文件名。我有一些大写字母的文件名:

xml -没有工作!

xml -正常工作!

可能发生这种情况的另一个原因是:今晚,我遇到了可怕的错误,最终发现一个错误的鼠标拖动显然将我自己的包的一个类移动到了另一个包的层次结构中。在Eclipse环境中,可以搞砸自己的方法确实有无数种。