初学者Android为什么要使用"id"是“未知成员"与R.id? [英] Beginner Android why is "id" is "Unknown member" with R.id?
问题描述
我确信这是一件非常简单的事情,因为这是我的第一个带有代码的android应用程序(我做的hello world示例只是将值赋给XML字符串).我的问题是,当尝试在变量中获取按钮的引用时,是否未定义R.id中的id?
I'm sure this is something dead simple as it's my first android app with code (the hello world example I did was just assigning a value to a string in XML). My problem is when trying to get the reference for my button in my variable then the id as in R.id is not defined?
在下面的代码中的注释中显示了编译器错误:
The compiler error is in a comment in the code below where it occurs:
package com.geeksonhugs.simplecalc;
import android.app.*;
import android.os.*;
import android.view.*;
import android.widget.*;
public class MainActivity extends Activity
{
private Button AddButton;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
AddButton = (Button)findViewById(R.id.btnAdd);
//"Unknown member 'id' of 'com.geeksonhugs.simplecalc.R'"
//AddButton.setOnClickListener(this);
}
}
XML布局文件:
<?xml version='1.0' encoding='utf-8' ?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LayoutRoot"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/txtFirstNum"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="@string/strFirstNum" />
<EditText
android:id="@+id/edtFirstNum"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="" />
<TextView
android:id="@+id/txtSecondNum"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="@string/strSecondNum" />
<EditText
android:id="@+id/edtSecondNum"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="" />
<Button
android:id="@+id/btnAdd"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="@string/strAdd"
android:gravity="center" />
<TextView
android:id="@+id/txtResult"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="" />
</LinearLayout>
推荐答案
R
类是在构建项目时由Android构建工具为您生成的代码.这意味着有四种可能性:
The R
class is code-generated for you by the Android build tools when you build your project. This means there are four possibilities:
-
您还没有尝试构建项目,在这种情况下,请尝试一下.
You have not tried building the project yet, in which case, try that.
Eclipse出于某种原因认为不需要构建项目-尝试从Eclipse菜单中选择项目">清理"(当然,仅在使用Eclipse时才相关).
Eclipse doesn't think it needs to build the project for some reason -- try Project > Clean from the Eclipse menu (only relevant if you are using Eclipse, of course).
清单或您的资源之一中存在某些错误,阻止了 R
的构建.为此,应该有Eclipse错误指示器.
There is some bug in your manifest or one of your resources that is preventing R
from being built. There should be Eclipse error indicators for this.
您的布局中没有 android:id
属性,因此没有 R.id
.
You do not have an android:id
attribute in your layout, and therefore there is no R.id
available.
这篇关于初学者Android为什么要使用"id"是“未知成员"与R.id?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!