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:
public void updateText(View view) {
TextView t = (TextView)findViewById(R.id.button);
t.setText("Updated!");
}
And the following bit ox XML in my layout (inside a LinearLayout):
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button"
android:onClick="updateText"
/>
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:
<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.