我刚刚下载并安装了新的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,如果它有,然后删除它,然后按Ctrl + Shift + o,选择packagename。R不导入另一个。

其他回答

首先检查是否有任何错误在任何xml布局,如果然后解决它首先。

否则,从项目中删除junit依赖项并重新构建项目。

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

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

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

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

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

宾果!

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.

只是清理和重建你的项目有时会有帮助

这可能是由于手动重命名AndroidManifest.xml中的包而导致的问题

另一种选择是使用内置工具重命名包,这将照顾到大多数问题,避免R not found问题。

Right Click on the project
Click on Android Tools
Click on Rename Application Project
Enter the new package name

然后将出现一个弹出窗口,要求应用更改,包括R工作区。

在android中重命名包名