2011-03-17 66 views
1

我想在我的應用程序中使自定義畫廊。我創建擴展庫類,但是當我試圖從我的XML得到了畫廊,我收到了ClassCastException類與自定義畫廊拋出異常android

這裏是我想做

MyCustomGallery mcg = (MyCustomGallery)findViewById(R.id.gallery);; 
d.setAdapter(new MyAdapter(getApplicationContext(), images)); 

這是我的自定義庫類

public class MyCustomGallery extends Gallery { 

    public MyCustomGallery (Context context) { 
     super(context); 
    } 

    @Override 
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { 
     return super.onFling(e1, e2, 100, 100); 

    } 
} 

,這是我的xml:

<Gallery android:id="@+id/gallery" 
     android:background="#000000" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:spacing="20dp" 
    /> 

我做錯了什麼?

在此先感謝。

回答

4

您已經在xml中聲明瞭它是一個普通的圖庫,您需要將其聲明爲您的自定義圖庫。

<com.yourPackage.MyCustomGallery android:id="@+id/gallery" 
     android:background="#000000" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:spacing="20dp" 
    /> 

請注意,您必須在xml中的聲明中包含完整的軟件包名稱,否則它將無法正常工作。

+0

我其實已經做到了,但我得到了這個:'NoSuchMethodException:MyCustomGallery(Context,AttributeSet)'有什麼想法? – madcoderz 2011-03-17 19:26:57

+0

對於愚蠢的問題感到抱歉:它是固定的,我只是把它要求'MyCustomGallery(Context,AttributeSet)'的構造函數。問題修復了,謝謝你的回答。 – madcoderz 2011-03-17 19:33:08