7
votes
12-01 00:36:28.058: E/AndroidRuntime(5062): Caused by: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.EditText

I am getting above error if anyone knows then tell me ...i shall very thankful


Java:

Log.d("Textra", title); 
Log.d("Dextra", des); 
EditText t=(EditText) findViewById(R.id.t); 
EditText d=(EditText) findViewById(R.id.des); 
t.setText(title); 
d.setText(des);

XML:

<LinearLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"> 

    <TextView 
        android:id="@+id/t" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="" 
        android:textAppearance="?android:attr/textAppearanceLarge" /> 

    <TextView 
        android:id="@+id/des" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text=""/> 

</LinearLayout>
5
The error is simple, but please post your relevant Java code, XML code, and all of the LogCat errors so we can where it is happening.Sam
java code: Log.d("Textra", title); Log.d("Dextra", des); EditText t=(EditText) findViewById(R.id.t); EditText d=(EditText) findViewById(R.id.des); t.setText(title); d.setText(des);Coder
<LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:id="@+id/t" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="" android:textAppearance="?android:attr/textAppearanceLarge" /> <TextView android:id="@+id/des" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text=""/> </LinearLayout>Coder
Use "edit" to add this information to your question in the appropirate format.Sam

5 Answers

46
votes

Delete R.java Clean project Save files Build & Run

6
votes
<TextView android:id="@+id/t" ... /> 
<TextView android:id="@+id/des" ... />
EditText t=(EditText) findViewById(R.id.t);
EditText d=(EditText) findViewById(R.id.des);

Do you want TextViews or EditTexts?

Either change the XML to use EditTexts or the Java to use TextViews...

4
votes

I could solve it by just cleaning the project with project/clean.

1
votes

If there is a cleanup issue then it can throw any casting error.I got this:

Caused by: java.lang.ClassCastException: android.widget.EditText cannot be cast to android.widget.LinearLayout

In eclipse, Just go to Project > Clean & Select your project to clean it.

0
votes

You use textview in xml, but in the activity you try to EditText t=(EditText) findViewById(R.id.t) - not correct; use TextView t=(TextView) findViewById(R.id.t);

or change in xml TextView to EditText