Reputation: 915
I am new to android development, and have been trying to use the beginner's tutorial as my starting point for developing a simple app. There is one screen, with an image, a row of four buttons, a textbox for the user to enter a pin, and a textview to display results.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/linear_layout_1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/imageView1"
android:contentDescription="@string/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/skytrek"
/>
<LinearLayout
android:id="@+id/linear_layout_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:text="@string/button_today"
android:onClick="today_click" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:text="@string/button_tomorrow"
android:onClick="tomorrow_click" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:text="@string/button_this_week"
android:onClick="this_week_click" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:text="@string/button_next_week"
android:onClick="next_week_click" />
</LinearLayout>
<EditText android:id="@+id/editText1"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:hint="@string/edit_message" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/init_message"/>
</LinearLayout>
I have code to deliver the results I want based on the button pressed - all fine. But then I wanted the user to enter a PIN as well as pressing a button, so I added the EditText control. This threw the following error:
E/AndroidRuntime(28578): Caused by: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.EditText
My Java class:
private String content = null;
private TextView textView1;
private EditText editText1;
public void today_click(View view) {
getPage("today");
}
public void tomorrow_click(View view) {
getPage("tomorrow");
}
public void this_week_click(View view) {
getPage("thisweek");
}
public void next_week_click(View view) {
getPage("nextweek");
}
public void getPage(String strParam) {
editText1 = (EditText) findViewById(R.id.editText1);
String message = editText1.getText().toString();
if (message.equals("4567")) {
content = "PIN recognised";
} else {
content = "PIN not recognised";
}
textView1 = (TextView) findViewById(R.id.textView1);
textView1.setText(content);
}
I thought I had done something silly, using the name of a TextView instead of an EditText control, but I can't find it if I have.
The error is being thrown at the line
getPage("thisweek");
I didn't understand how this line involved views of any sort, but of course the function heading
this_week_click(View view)
does, and when I changed the order of the TextView and the EditText in the XML file (so that the TextView comes first), the error disappeared. It is as if the "view" being passed is not the button, but the nearest widget to the button. I have read
existence of parameter (View view)
but it only seems to confirm my (mis)understanding that a button should be passed as the view parameter. I have also tried cleaning the project, and building a completely new project. What on earth is causing the casting error?
Upvotes: 1
Views: 128
Reputation: 14810
Try cleaning your project and run it again.. Eclipse -> Project menu ->Clean
It is because eclipse gets confused when we play around in the xml file ;)
Upvotes: 0
Reputation: 708
Everytime if you make any changes in xml or any interchange of position of views in xml or change of id's,you need to clean build your project.If not you will get this exception.
Upvotes: 2
Reputation: 2879
Hello I have tested your code, your code is fine. Please clean your project from
select your project then click on Project and then Clean and also check the Build Automatically . Your auto generated class R is not generated properly.
Upvotes: 1
Reputation: 1848
If you're using Eclipse, go to the menu voice "Project" and select "Clean"
Sometimes Eclipse has some problem with ids, by cleaning the project you regenerate them..
Upvotes: 5