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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13674627/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 19:32:29  来源:igfitidea点击:

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

androideclipse

提问by Coder

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:

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>

回答by AnDx

Delete R.java Clean project Save files Build & Run

删除 R.java Clean 项目 保存文件 Build & Run

回答by Sam

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

你想要 TextViews 还是 EditTexts?

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

将 XML 更改为使用 EditTexts 或将 Java 更改为使用 TextViews ...

回答by Allfarid Morales García

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

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

回答by Sumoanand

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.

在 eclipse 中,只需转到 Project > Clean & Select your project 来清理它。

回答by Yereke

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

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

or change in xml TextView to EditText

或将 xml TextView 更改为 EditText