Android 制作一个简单的安卓按钮

Android 制作一个简单的安卓按钮,android,Android,我正在试着做一个按钮。我总是收到一个R.id错误 "id cannot be resolved or is not a field." 此行抛出错误: LinearLayout layout = (LinearLayout) findViewById(R.id.layout1); 每次我试着用R来表示,它都会抛出同样的错误 如果我在R.java类中创建eclipse推荐的字段 public static Object id; 然后,eclipse高亮显示。布局1 错误是“layout1无法

我正在试着做一个按钮。我总是收到一个R.id错误

"id cannot be resolved or is not a field."
此行抛出错误:

LinearLayout layout = (LinearLayout) findViewById(R.id.layout1);
每次我试着用R来表示,它都会抛出同样的错误

如果我在R.java类中创建eclipse推荐的字段

public static Object id;
然后,eclipse高亮显示。布局1

错误是“layout1无法解析或不是字段。”

代码如下:

import android.widget.Button;

Button myButton = new Button(this);
myButton.setText("Press Me");

Finally add the button to the layout:

LinearLayout layout = (LinearLayout) findViewById(R.id.layout1);
layout.addView(myButton);

非常感谢您的帮助。有没有拖放控件的方法?为什么我不能使用R.id?

您的R似乎没有正确更新或生成

如果您将项目设置为Android项目,则Eclipse会自动生成R。它将检查XML并为您生成资源ID,以便您可以在代码中使用它。如果您已将项目设置为Android项目,请尝试project->Clean并查看是否有任何错误。如果在构建过程中没有错误,那么应该为您生成R类及其所有字段


有关其他帮助,请参见此部分。

在布局的XML定义中,它是否具有“android:id=“layout1”属性?这就是您为小部件ID命名的方式。

您在eclipse中创建了“Android项目”吗?是的,这正是我正在做的。我会发短信。然后你能检查一下你是否为你的线性布局添加了id吗?我让它工作了。我是android新手,我没有正确的id。