Android: java.lang.ClassCastException: android.widget.imageView cannot be cast to android.widget.textView

ぐ巨炮叔叔 提交于 2019-11-28 16:29:07

Eclipse tends to mess up your resources every now and then. This leads to some odd behavior such as strings and images being swapped all over your app, and more commonly classCastException(s), which happen when Eclipse switches your Views' ids around.

A few solutions to that problem:

Clean your project.

Modify an xml layout file and save.

Delete your R file. (Don't worry it will be automatically generated again).

Basically anything that makes your project rebuild and re-generate the R file.

Just clean up your project and everything will be fine.

Some times this problem shows because we have created ImageView But Cast with Image Button as like this

 <ImageView
        android:id="@+id/iv_thumb"
        android:layout_width="50dip"
        android:layout_height="50dip"
        />

  holder.imageView = (ImageButton) convertView.findViewById(R.id.iv_thumb);

we have declared Imageview but cast to ImageButton, for this reason also have face this problem.

touchchandra

Initially, I was getting error:

TextView : android:id="@+id/add_expected_seekbarvalue"

SeekBar : android:id="@+id/add_expected_seekbar"

As a fix, I renamed the id for TextView as

TextView : android:id="@+id/add_expected_valueseekbar"

Even clean up and other techniques didn't work but finally renaming the id worked like a charm.

for Visual Studio Xamarin.Android Development

Like mentioned by many, Cleaning the project did the trick for me on Visual Studio For my Xamarin.Forms app, it happened after pulling in changes with new library reference.

Clean your project.

Basically anything that makes your project rebuild and re-generate the Rosource.Designer.cs file. (Burrowed from @Benito Bertoli)

just clean your project... I also gone through similar problem.. after clean project everything will be fine..best luck

Maybe you have an image view in your .xml file and a button which dynamically generated and given an id in your java code and these two randomly get a same id and because of that you get this error. try to change the id which you have given in your code and it will be fine.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!