Search code examples
androidandroid-gallery

ClassCastException (Gallery + ImageView)


I have followed a tutorial to create a gallery with an imageview below it. All I want to do is place the gallery on the bottom and the imageview above it, but when I switch them around in the XML it gives me a ClassCastException.

Here is the XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/backrepeat"
android:orientation="vertical" >

<ImageView
    android:id="@+id/ImageView01"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

<Gallery
    android:id="@+id/gallery"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />

</LinearLayout>

That is the non-working version, but placing gallery above imageview works.

From log:

01-26 22:20:13.673: E/AndroidRuntime(349): Caused by: java.lang.ClassCastException:         android.widget.ImageView
01-26 22:20:13.673: E/AndroidRuntime(349):  at     com.appinfluence.fanapp.v1.Photos.onCreate(Photos.java:36)

onCreate:

Gallery gallery = (Gallery) findViewById(R.id.gallery);
    gallery.setAdapter(new ImageAdapter(Photos.this));

    imgView = (ImageView)findViewById(R.id.ImageView01);
    imgView.setImageResource(mImageIds[0]);


    gallery.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView parent, View v, int position, long id) {
            imgView.setImageResource(mImageIds[position]); 
        }
    });
}

Other helpful code:

public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView = new ImageView(mContext);

        imageView.setImageResource(mImageIds[position]);
        imageView.setLayoutParams(new Gallery.LayoutParams(150, 100));
        imageView.setScaleType(ImageView.ScaleType.FIT_XY);
        imageView.setBackgroundResource(mGalleryItemBackground);

        return imageView;
    }   

http://4.bp.blogspot.com/_I2Ctfz7eew4/S_JxIfgKrfI/AAAAAAAAAzA/m1KUSIugojY/s1600/Gallery2.1.PNG


Solution

  • Hey NickNelson Just now i got the solution to your problem, just clean the project it will work.

    Sample screenshot

    enter image description here