我刚刚下载并安装了新的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未生成甚至消失的问题时,这都是由于XML布局文件中的某些问题导致应用程序无法构建。

其他回答

在许多可能导致这种情况的问题中,这是为我解决的问题。

右键单击项目->属性-> Java构建路径->源。

然后添加gen和src文件夹。出于某种原因,Android希望我只保存整个文件夹。

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

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

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

import android.R;

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

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

检查上面的类文件导入,确保没有android的导入。R,如果它有,然后删除它,然后按Ctrl + Shift + o,选择packagename。R不导入另一个。