从 R.id.* 我可以访问 txtWeight、txtLength、lblWeight 和 lblLength,仅此而已。不能从 Java 代码访问所有其他对象。
TextView lblAverage = (TextView) findViewById (R.id.lblAverage);
lblAverage cannot be resolved or is not a field
这是整个布局文件;
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="4dp" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="@drawable/fts logo" />
<TextView
android:id="@+id/lblWeight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/imageView1"
android:layout_below="@+id/imageView1"
android:layout_marginTop="21dp"
android:text="Weight:"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="@+id/txtWeight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/lblWeight"
android:layout_alignBottom="@+id/lblWeight"
android:layout_alignParentRight="true"
android:ems="10"
android:inputType="numberDecimal" />
<TextView
android:id="@+id/lblLength"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/lblWeight"
android:layout_below="@+id/txtWeight"
android:layout_marginTop="24dp"
android:text="Length:"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="@+id/txtLength"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/lblLength"
android:layout_alignBottom="@+id/lblLength"
android:layout_alignLeft="@+id/txtWeight"
android:ems="10"
android:inputType="numberDecimal" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/lblLength"
android:layout_centerHorizontal="true"
android:ellipsize="none"
android:paddingTop="16dp"
android:text="Weight in kilograms, length in meters" />
<Button
android:id="@+id/btnCalculate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="16dp"
android:paddingLeft="24dp"
android:paddingRight="24dp"
android:text="Calculate"
android:onClick="btnCalculate_clicked" />
<TextView
android:id="@+id/lblBMI"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/btnCalculate"
android:layout_centerHorizontal="true"
android:text="Your BMI is"
android:textAppearance="?android:attr/textAppearanceLarge"
android:visibility="invisible" />
<TextView
android:id="@+id/lblComparedToIndex"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/lblBMI"
android:layout_below="@+id/lblBMI"
android:layout_marginTop="14dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:visibility="invisible" />
<Button
android:id="@+id/btnSubmitToIndex"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/lblComparedToIndex"
android:layout_centerHorizontal="true"
android:text="Submit to index"
android:visibility="invisible"
android:onClick="btnSubmitToIndex_clicked" />
<TextView
android:id="@+id/lblAverage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="Average BMI: ?"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>