简体   繁体   中英

java.lang.ClassCastException: android.widget.TextView cannot be cast

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>

删除R.java清理项目保存文件构建和运行

<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...

我可以通过使用project / clean清理项目来解决它。

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM