繁体   English   中英

java.lang.ClassCastException:无法转换android.widget.TextView

[英]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

如果有人知道然后告诉我,我会犯错误...我将非常感谢


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);

你想要TextViews还是EditTexts?

更改XML以使用EditTexts或Java来使用TextViews ...

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

如果有一个清理问题,那么它可以抛出任何转换错误。我得到了这个:

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

在eclipse中,只需转到Project> Clean&Select您的项目进行清理。

你在xml中使用textview,但在你尝试EditText的活动中t =(EditText)findViewById(R.id.t) - 不正确; 使用TextView t =(TextView)findViewById(R.id.t);

或者将xml TextView更改为EditText

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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