I am new to Android programming and so kingly pardon me if this question sounds stupid. I am developing a Calculator for Android. So I have around 20 buttons and a text field. Whenever a button is pressed I update the text field so that the user knows what he has typed.
However when I update the textfield the application is crashing.
updateExpression is the method that is called when any button is pressed.
public void updateExpression (View v)
{
Log.d("string", "updateExpression Called");
EditText text = (EditText) findViewById(R.id.name);
text.setText("HELLO EVERYONE");
}
The .xml file of the buttons is:
<Button
android:id="@+id/Button03"
android:layout_width="70dp"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/name"
android:layout_below="@+id/name"
android:onClick="updateExpression"
android:text="C" />
// goes on for 20 buttons with different ids and text
<TextView
android:id="@+id/name"
android:layout_width="fill_parent"
android:text="@string/edit_message"
android:layout_height="33dp"
android:textColor="@color/opaque_red"/>
</RelativeLayout>
Finally the logcat has the string data “updateExpression Called”. However immediately after that it crashes. The error has tag AndroidRuntime Fatal Exception main. There are many more errors after this. A relevant one (according to me is)
Caused by: java.lang.classCatException: android.widget.TextView
cannot be cast into android.widget.EditText
Kindly help.
Thank you.
You are trying to cast a TextView into an EditText, like the error says. These are two seperate classes.
should be