2012-01-27 29 views
0

我已經按照教程創建了一個帶有imageview的圖庫。我想要做的就是將圖庫放置在底部,並將圖像放置在圖像上方,但是當我在XML中切換它時,它會給我一個ClassCastException。ClassCastException(Gallery + ImageView)

這裏是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> 

即非工作版本,但將上述imageview的作品的畫廊。

從日誌:

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

其他有用的代碼:

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

+0

是啊,我也獲得同樣的問題,如果ImageView的是上面和畫廊如下。 – 2012-01-27 05:34:34

+0

你能告訴我們這段代碼:com.appinfluence.fanapp.v1.Photos.onCreate(Photos.java:36) – 2012-01-27 05:45:30

+0

它已經存在 – 2012-01-27 06:06:47

回答

0

嘿NickNelson剛纔我得到了解決你的問題,只是清理項目它將工作。

樣品截圖

enter image description here

+0

爲什麼在頂部有差距? ** 4/7 **和Google圖片之間的差距? – 2012-01-27 06:25:22

+0

?由於圖像高度。 – 2012-01-27 07:05:38

+0

檢查現在替換截圖。 – 2012-01-27 07:08:06

0

無需使用單獨的ImageView在佈局中,圖庫將顯示圖像。取出ImageView並檢查它..

編輯

剛剛清理項目。 (如yugandhar說。)

+0

這是沒有意義的,因爲我使用imageview來顯示圖像點擊時畫廊。我使用這個教程:http://saigeethamn.blogspot.com/2010/05/gallery-view-android-developer-tutorial.html – 2012-01-27 05:14:30

+0

你是否獲得畫廊iamges。 – 2012-01-27 05:21:14

+0

是的,我正在從一系列drawables中拉出圖像,它看起來像我要在我的問題中編輯的圖片,但我想要顛倒它們的順序。 – 2012-01-27 05:25:35

0

請試試這個XML的佈局部分:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:gravity="center_vertical"> 

    <ImageView android:layout_alignParentTop="true" 
     android:id="@+id/ImageView01" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:src="@drawable/antartica1"/> 

    <Gallery 
     android:id="@+id/Gallery01" 
     android:layout_width="fill_parent" 
     android:layout_height="150dp" 
     android:layout_alignParentBottom="true" /> 

</RelativeLayout>