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


当前回答

发生这种情况的另一种方式是,从其中一个示例启动一个新项目。当您稍后决定将包名从com.example.android.foo更改为您自己的域时,您将需要修改清单和单个.java文件中的几个值。

如果您使用的是Eclipse,请找到.java文件的包语句并选择QuickFix。这里可能有几个选项,但你想要的是指示“移动‘foo.java’到打包‘com.youdomain.android.yourapp’”的选项。保存文件,它可以自动构建或做其他人建议,并尝试“项目->清洁”。

其他回答

发生这种情况的另一种方式是,从其中一个示例启动一个新项目。当您稍后决定将包名从com.example.android.foo更改为您自己的域时,您将需要修改清单和单个.java文件中的几个值。

如果您使用的是Eclipse,请找到.java文件的包语句并选择QuickFix。这里可能有几个选项,但你想要的是指示“移动‘foo.java’到打包‘com.youdomain.android.yourapp’”的选项。保存文件,它可以自动构建或做其他人建议,并尝试“项目->清洁”。

还有一件事可能会导致这个问题:

我安装了新的ADT(第22节)。它停止创建包含R.java的gen文件夹。解决方案是在Android SDK管理器中安装新的Android SDK构建工具。

在这里找到解决方案

R.java是Android Eclipse插件创建的文件 构建应用程序。R.java是在“gen”下创建的 目录中。此文件由“res”中的信息生成。 目录中。如果在Eclipse上运行select“Project”->“Clean… 菜单,它将删除,然后重新生成R.java文件。

“R不能被解决”的问题发生在你改变你的 在AndroidManifest.xml文件中的包名。它使用安卓系统 包名下创建子目录“gen”目录,其中 它存储R.java文件。

Eclipse may have problems executing clean, because it is confused about where the R.java file is when you have changed the Android package name. You can either rename the subdirectory under gen to match your new package name, or you can change your package name back to the old name. Do the clean and then change the package name to the new name you want. This works best if you stop Eclipse from trying to build while you are changing the package name. Under the "Project" menu uncheck the option to "Build Automatically" and also when the "Clean..." dialog asks if it should "Start a build immediately" uncheck the box so it doesn't try to build while you are changing the package name. After you have changed the name you can turn "Build Automatically" back on again.

Note that if your AndroidManifest.xml file package name does not match your Java package name, Eclipse will end up automatically adding an "import <your Android package name>.R;" line in all your .java files that have any references to R. If you change your AndroidManifest.xml package name, sometimes Eclipse does not update all of these added imports. If that happens, use the Eclipse refactoring (ALT + Shift + R) to change the import statement in one of your Java files to your new AndroidManifest.xml package name. It is best to do this while you have disabled "Build Automatically".

哇。我通过在所有XML文件和相关资源中跟踪所有可能的错误来解决R问题。我知道这是一些层次,但简单地说,你必须:

确保所有引用的资源映像都在那里 大家都已经提到这么多了,这只是回声…如果需要,删除所有Eclipse注入的R.*导入。 在执行上述操作时,我喜欢重新启动Eclipse。 确保所有XML文件的结构正确。就像,做你的事情。哈哈哈 尽量多写几行。养成经常按Ctrl + Shift + S的习惯,可以快速改变编译器的结果。

Hahah。当然,得到R是很好的,但不能解决固定…

我猜你只是复制粘贴了密码,或者没有。无论哪种方法,检查AndroidManifest.xml文件,看看包名是否正确。