tags:

views:

12

answers:

1

Hi, Made a very basic xml file to try and access a simple button widget. The main.xml file is:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:background="@color/white">
  <TextView android:layout_width="fill_parent" 
            android:layout_height="wrap_content" 
            android:text="@string/hello"/>
  <TextView android:text="Heading Text" 
            android:id="@+id/TextView01" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content">
  </TextView>
  <Button android:text="Button Text" 
          android:id="@+id/Button01" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content">
  </Button>
</LinearLayout>

The Java program is

import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;


public class TestButton extends Activity {

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Button myButton = (Button)findViewById(android.R.id.Button01);

    }
}

The error on the 'Button' line is:
Button01 cannot be resolved or is not a field

Any ideas, what very basic mistake am I making :( :(

Oliver

+1  A: 
Button myButton = (Button)findViewById(android.R.id.Button01);

This is wrong. It is not referencing the auto-generated R.java from your project, it is referencing the standard android.R class. It should be:

Button myButton = (Button) findViewById(R.id.Button01);
Dan Dyer
I owe you one! Eclipse was insisting on putting in the 'android' prefix and, being new to this platform/toolset didnt realise I could simply delete teh 'android' reference. Everything working just fine now. Thanks
LenseOnLife