by

Eclipse doesn't let me to findViewById

Yesterday I did my first bit of Android development ever. I created a very simple program that simply updated some text on screen.

I had a problem though. On my Activity, I had the following piece of code:

1public void updateText(View view) {
2  TextView t = (TextView)findViewById(R.id.button);
3  t.setText("Updated!");
4}

And the following bit ox XML in my layout (inside a LinearLayout):

1<Button
2  android:id="@+id/button"
3  android:layout_width="wrap_content"
4  android:layout_height="wrap_content"
5  android:text="@string/button"
6  android:onClick="updateText"
7/>

Eclipse insisted that R.id was illegal. Specifically, the error was id cannot be resolved or is not a field, even though the button did exist in the view.

The problem was that I had not yet defined the string "button" in my strings.xml file:

1<string name="button">Click me!</string>

Not sure of the exact internals, but I guess that Eclipse was trying to compile the layout, and it failed because the string was missing. In turn, this showed an error in the activity, because it was using a broken layout.