简体   繁体   English

Android: java.lang.ClassCastException: android.widget.imageView 无法转换为 android.widget.textView

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

I can't fix this problem on my listview template: i have the error as in the title of my post, but i won't cast imageview to textview.我无法在我的 listview 模板上解决这个问题:我有帖子标题中的错误,但我不会将 imageview 转换为 textview。 Here's my code:这是我的代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="200dip"
android:layout_weight="1"
android:paddingBottom="10dip" >

<TextView
    android:id="@+id/mq"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:text="Metri quadri"
    android:textColor="#33b5e5"
    android:textSize="14sp"
    android:textStyle="bold" />

<TextView
    android:id="@+id/citta"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/mq"
    android:layout_alignBottom="@+id/mq"
    android:layout_centerHorizontal="true"
    android:text="Citta"
    android:textColor="#33b5e5"
    android:textSize="14sp"
    android:textStyle="bold" />

<TextView
    android:id="@+id/prezzo"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:text="Prezzo"
    android:textColor="#e1e1e1"
    android:textSize="14sp"
    android:textStyle="bold" />

<TextView
    android:id="@+id/nome"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:padding="5dip"
    android:scrollHorizontally="true"
    android:singleLine="true"
    android:text="@string/testolungo"
    android:textColor="#e1e1e1"
    android:textSize="20sp"
    android:textStyle="bold" />

<ImageView
    android:id="@+id/foto"
    android:layout_width="80dip"
    android:layout_height="60dip"
    android:layout_alignParentLeft="true"
    android:layout_centerVertical="true"
    android:contentDescription="Release"
    android:padding="5dip"
    android:scaleType="centerCrop"
    android:src="@drawable/stub" />

<TextView
    android:id="@+id/descsplash"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:text="Small Text"
    android:textSize="16sp"
    android:textStyle="italic" />

</RelativeLayout>

The error for the java code is on line 58 of my file, where i assign the TextView id to TextView mq: java 代码的错误在我的文件的第 58 行,我在其中将 TextView id 分配给 TextView mq:

    View vi=convertView;
    if(convertView==null)
    vi = inflater.inflate(R.layout.listlay, null);
    TextView nome=(TextView)vi.findViewById(R.id.nome);
    TextView mq=(TextView)vi.findViewById(R.id.mq); // here's the error (???)
    TextView citta=(TextView)vi.findViewById(R.id.citta);
    TextView prezzo=(TextView)vi.findViewById(R.id.prezzo);
    ImageView image=(ImageView)vi.findViewById(R.id.foto);
    TextView descrizione = (TextView)vi.findViewById(R.id.descsplash);
    nome.setText(data.get(position).getNome());
    mq.setText(data.get(position).getMetriQuadri());
    citta.setText(data.get(position).getCitta());
    prezzo.setText(data.get(position).getPrezzo());
    descrizione.setText(data.get(position).getDescrizione());
    imageLoader.DisplayImage(data.get(position).getForoUrl(), image);

And here is the logcat:这是 logcat:

06-25 16:08:32.497: D/Debug(14642): Prendo Textview MQ 06-25 16:08:32.497: D/调试 (14642): Prendo Textview MQ
06-25 16:08:32.497: D/AndroidRuntime(14642): Shutting down VM 06-25 16:08:32.497: D/AndroidRuntime (14642): 关闭虚拟机
06-25 16:08:32.497: W/dalvikvm(14642): threadid=1: thread exiting with uncaught exception (group=0x40a561f8) 06-25 16:08:32.497: W/dalvikvm(14642): threadid=1: 线程以未捕获的异常退出 (group=0x40a561f8)
06-25 16:08:32.497: E/AndroidRuntime(14642): FATAL EXCEPTION: main 06-25 16:08:32.497:E/AndroidRuntime(14642):致命异常:主要
06-25 16:08:32.497: E/AndroidRuntime(14642): java.lang.ClassCastException: android.widget.ImageView cannot be cast to android.widget.TextView 06-25 16:08:32.497: E/AndroidRuntime(14642): java.lang.ClassCastException: android.widget.ImageView 无法转换为 android.widget.TextView
06-25 16:08:32.497: E/AndroidRuntime(14642): at com.prova.listview.LazyAdapter.getView(LazyAdapter.java:58) 06-25 16:08:32.497:E/AndroidRuntime(14642):在 com.prova.listview.LazyAdapter.getView(LazyAdapter.java:58)

Does anyone know how i can fix this problem?有谁知道我该如何解决这个问题?

Eclipse tends to mess up your resources every now and then. Eclipse 往往会时不时地弄乱您的资源。 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.这会导致一些奇怪的行为,例如在整个应用程序中交换字符串和图像,以及更常见的 classCastException(s),当 Eclipse 切换视图的 id 时会发生这种情况。

A few solutions to that problem:该问题的一些解决方案:

Clean your project.清理你的项目。

Modify an xml layout file and save.修改一个xml布局文件并保存。

Delete your R file.删除您的 R 文件。 (Don't worry it will be automatically generated again). (不用担心它会再次自动生成)。

Basically anything that makes your project rebuild and re-generate the R file.基本上任何使您的项目重建和重新生成 R 文件的东西。

Just clean up your project and everything will be fine.只需清理您的项目,一切都会好起来的。

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作为修复,我将TextViewid重命名为

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.甚至清理和其他技术都不起作用,但最终重命名 id 就像一个魅力。

Some times this problem shows because we have created ImageView But Cast with Image Button as like this有时会出现此问题,因为我们创建了 ImageView But Cast with Image Button 就像这样

 <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.我们已经声明了 Imageview 但转换为 ImageButton,因此也面临这个问题。

for Visual Studio Xamarin.Android Development适用于Visual Studio Xamarin.Android 开发

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.正如许多人提到的那样,清理项目在 Visual Studio 上对我有用。对于我的 Xamarin.Forms 应用程序,它发生在使用新库引用引入更改之后。

Clean your project.清理你的项目。

Basically anything that makes your project rebuild and re-generate the Rosource.Designer.cs file.基本上任何使您的项目重建和重新生成 Rosource.Designer.cs 文件的东西。 (Burrowed from @Benito Bertoli) (来自@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.也许您的 .xml 文件中有一个图像视图和一个动态生成的按钮,并在您的 java 代码中给出了一个 id,这两个随机获得相同的 id,因此您会收到此错误。 try to change the id which you have given in your code and it will be fine.尝试更改您在代码中提供的 ID,这样就可以了。

暂无
暂无

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

相关问题 java.lang.ClassCastException:无法转换android.widget.TextView - java.lang.ClassCastException: android.widget.TextView cannot be cast Android java.lang.ClassCastException:android.widget.LinearLayout无法强制转换为android.widget.TextView - Android java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.TextView java.lang.ClassCastException:android.widget.TextView无法转换为android.widget.EditText - java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.EditText java.lang.ClassCastException:android.widget.LinearLayout 无法转换为 android.widget.TextView - java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.TextView java.lang.ClassCastException:android.widget.ScrollView无法强制转换为android.widget.TextView - java.lang.ClassCastException: android.widget.ScrollView cannot be cast to android.widget.TextView 造成原因:java.lang.ClassCastException:android.widget.RelativeLayout无法转换为android.widget.TextView - Caused by: java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.TextView java.lang.ClassCastException:android.widget.ImageButton无法转换为android.widget.TextView - java.lang.ClassCastException: android.widget.ImageButton cannot be cast to android.widget.TextView java.lang.ClassCastException:android.widget.TextView无法转换为android.widget.EditView - java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.EditView java.lang.ClassCastException: androidx.appcompat.widget.AppCompatImageView 不能转换为 android.widget.TextView - java.lang.ClassCastException: androidx.appcompat.widget.AppCompatImageView cannot be cast to android.widget.TextView java.lang.ClassCastException:android.support.constraint.ConstraintLayout无法转换为android.widget.TextView - java.lang.ClassCastException: android.support.constraint.ConstraintLayout cannot be cast to android.widget.TextView
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM