InvalidCastException when using FindViewById

TimHaerkensTimHaerkens USMember
edited September 2016 in Xamarin.Android

Hi,

So I've picked up coding on an app that has already been released and all, but before I'm going to work on it I have to get it working.
But everytime I'm debugging I get an InvalidCastException:

System.InvalidCastException: Unable to convert instance of type 'Android.Support.V7.Widget.AppCompatImageView' to type  'Freshheads.FestiApp.Droid.GraphImageView'.

The code:

    public GraphImageView LogoImage { get; private set; }

public FestivalViewHolder (View itemView) : base (itemView)
{
    this.AssignResourceProperties (itemView);

    CardView = itemView.FindViewById<CardView> (Resource.Id.cardView);
    CardView.SetOnClickListener (this);

    TitleLabel.Typeface = Typefaces.PTSansBold;
    CategoryLabel.Typeface = Typefaces.PTSansRegular;
    DateLabel.Typeface = Typefaces.PTSansBold;

    //Unhandled Exception here making the app crash
    LogoImage = itemView.FindViewById<GraphImageView> (Resource.Id.logoImage); //this line
    LogoImage.SetImageResource (Resource.Drawable.placeholder);

    if (!addButtonEventAdded)
                    {
        AddImage.Click += HandleAddButtonClick;
        addButtonEventAdded = true;
    }
}

Go on and ask me for more details on specific things because I'm not super familiar with the project yet and I don't know what code is custom and what not.

Best Answer

Answers

  • MabroukMabrouk USMember ✭✭✭

    Hi,

    That comes because you're using an AppCompatImageView in your layout. To fix it , try a GraphImageView

  • The project has several axml files. All I can find in these is one GraphImageView:

    <Freshheads.FestiApp.Droid.GraphImageView
            android:id="@+id/imageView"
            android:src="@android:drawable/ic_menu_gallery"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_gravity="left|center"
            android:scaleType="centerCrop"
            android:layout_marginTop="15dp"
            android:layout_marginBottom="15dp" />
    

    Also, searching for AppCompatImageView results nothing but an int array named AppCompatImageView, whereas GraphImageView as a type is used a bunch of times in the project.

    The int array is used in Resource.designer.cs:

    public static int[] AppCompatImageView = new int[] 
    {
        16843033,
        2130772065
    };
    
Sign In or Register to comment.