Android: java.lang.ClassCastException: android.widget.imageView 不能转换为 android.widget.textView [英] Android: java.lang.ClassCastException: android.widget.imageView cannot be cast to android.widget.textView

查看:15
本文介绍了Android: java.lang.ClassCastException: android.widget.imageView 不能转换为 android.widget.textView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法在我的 listview 模板上解决此问题:我的帖子标题中出现错误,但我不会将 imageview 转换为 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. 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>

java 代码的错误在我文件的第 58 行,我将 TextView id 分配给 TextView mq:

The error for the java code is on line 58 of my file, where i assign the TextView id to 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);

这里是 logcat:

And here is the logcat:

06-25 16:08:32.497:D/Debug(14642):Prendo Textview MQ
06-25 16:08:32.497: D/AndroidRuntime(14642): 关闭 VM
06-25 16:08:32.497: W/dalvikvm(14642): threadid=1: 线程以未捕获的异常退出 (group=0x40a561f8)
06-25 16:08:32.497: E/AndroidRuntime(14642): 致命异常: main
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: D/Debug(14642): Prendo Textview MQ
06-25 16:08:32.497: D/AndroidRuntime(14642): Shutting down VM
06-25 16:08:32.497: W/dalvikvm(14642): threadid=1: thread exiting with uncaught exception (group=0x40a561f8)
06-25 16:08:32.497: E/AndroidRuntime(14642): FATAL EXCEPTION: main
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): at com.prova.listview.LazyAdapter.getView(LazyAdapter.java:58)

有谁知道我该如何解决这个问题?

Does anyone know how i can fix this problem?

推荐答案

Eclipse 往往会时不时地弄乱你的资源.这会导致一些奇怪的行为,例如在整个应用程序中交换字符串和图像,以及更常见的 classCastException(s),当 Eclipse 切换视图的 id 时会发生这种情况.

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:

清理你的项目.

修改一个xml布局文件并保存.

Modify an xml layout file and save.

删除您的 R 文件.(不用担心它会再次自动生成).

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

基本上任何使您的项目重建和重新生成 R 文件的东西.

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

这篇关于Android: java.lang.ClassCastException: android.widget.imageView 不能转换为 android.widget.textView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆