60

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>

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

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

assylias
  • 321,522
  • 82
  • 660
  • 783
Release
  • 1,167
  • 1
  • 12
  • 21
  • 7
    Seems correct. Just try to clean your project. – Praveenkumar Jun 25 '12 at 14:18
  • can you post the catlog? A clean may be what you need as @SpK recommends – MikeIsrael Jun 25 '12 at 14:25
  • 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) – Release Jun 25 '12 at 14:27
  • 1
    thank you guys! how can i clean the project? i'm new in android programming – Release Jun 25 '12 at 14:28
  • @Release You can edit your post to add information at any time. I have added the logcat to your post for you. – assylias Jun 25 '12 at 14:32
  • Project > Clean... Select your project and click OK. – Benito Bertoli Jun 25 '12 at 14:32
  • 1
    @assylias i'm sorry, now i know it. By the way i solved my problem deleting my R file. Thank you – Release Jun 25 '12 at 14:46

7 Answers7

180

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.

Benito Bertoli
  • 25,285
  • 12
  • 54
  • 61
4

Just clean up your project and everything will be fine.

Aïssa
  • 670
  • 8
  • 10
1

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.

Satan Pandeya
  • 3,747
  • 4
  • 27
  • 53
touchchandra
  • 1,506
  • 5
  • 21
  • 37
1

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.

Rohit
  • 3,401
  • 4
  • 33
  • 60
1

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)

Ahmed Alejo
  • 2,334
  • 2
  • 16
  • 16
0

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

Amol Suryawanshi
  • 2,108
  • 21
  • 29
0

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.

Atieh mn
  • 183
  • 3
  • 13