2011-11-27 28 views
0

我有問題使用我的自定義按鈕類。 我已經嘗試將xml佈局從<Button/>更改爲<gButton/>,但那沒有奏效。 我也試過鑄造Button page1 = (gButton) findView....,在第15行,但由於某種原因不能正常工作(你可以投一個子類,對吧?)Android:按鈕類ClassCastException

我一直從logcat中得到的錯誤是:

Caused by: java.lang.ClassCastException: android.widget.Button 
    at flash.tut.ButtonPage.onCreate(ButtonPage.java:15) 

任何幫助非常感謝。

這裏是我的按鈕看法:

public class ButtonPage extends Activity{ 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.button); 

     gButton page1 = (gButton) findViewById(R.id.Button01); //<---LogCat: ClassCastException 
     page1.setOnClickListener(new View.OnClickListener() { 
       public void onClick(View view) { 
        Intent mI = new Intent(view.getContext(), Page1.class); 
        startActivityForResult(mI, 0); 
       } 
     }); 
    } 
} 

和我gButton延伸按鈕類:

public class gButton extends Button{ 
    private static Context con; 

    public gButton(){this("Blank");} 

    public gButton(String name){ 
     this(name, R.color.text_color); 
    } 

    public gButton(String name, int col) { 
     this(name, col, con); 
    } 

    public gButton(String name, int col, Context context) { 
     super(context); //necessary context...dont know why. 
     setBackgroundResource(R.drawable.icon); 
     setTextColor(col); 
     setText(name); 
     setTextSize(14); 
    } 
} 

,最後我的XML佈局:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

<Button android:text="Page 1" 
     android:id="@+id/Button01" 
     android:layout_width="250px" 
     android:textSize="18px" 
     android:layout_height="55px"> 
    </Button> 

<Button android:text="Page 2" 
     android:id="@+id/Button02" 
     android:layout_width="250px" 
     android:textSize="18px" 
     android:layout_height="55px"> 
    </Button> 

<Button android:text="Page 3" 
     android:id="@+id/Button03" 
     android:layout_width="250px" 
     android:textSize="18px" 
     android:layout_height="55px"> 
    </Button> 

<Button android:text="ListView Page" 
     android:id="@+id/ButtonList" 
     android:layout_width="250px" 
     android:textSize="18px" 
     android:layout_height="55px"> 
    </Button> 

</LinearLayout> 

回答

1

Button01是一個按鈕,你正在試圖將一個按鈕轉換爲gbutton.You不能將一個對象轉換成一個派生對象,除非是對象是派生對象。例如,您有一個名爲Product的基類,它由CartItem擴展。

現在,還可以設想產品除CartItem之外還有其他派生類,例如PhoneItem和MailOrderItem。

您不能這樣做: Product prod = new Product(); CartItem cart =(CartItem)prod。

爲什麼?

因爲「prod」不是一個CartItem,它不僅僅是一個PhoneItem和一個MailOrderItem。您將其創建爲「產品」項目,無論您如何投射,它總是成爲產品項目。你可以強制它成爲一個MailItem或一個PhoneItem。但是,如果你做了以下工作,它的工作原理如下: CartItem cart = new CartItem(); 產品prod =(產品)購物車; CartItem cart2 =(CartItem)prod;

產品prod = new CartItem(); //(隱式投射到產品) CartItem cart2 =(CartItem)prod;

爲什麼這樣嗎?因爲CartItem也是一個產品(儘管並非所有產品都是購物車產品)。即使你把它鑄造成一個產品,它仍然是,並且總是一個CartItem。唯一的區別是CartItem的當前句柄恰好是一個Product句柄,使您可以更加有限地訪問CartItem的所有功能。當您嘗試將其從產品轉換回CartItem時,運行時引擎會說:「嘿,我可以將此目標對象轉換爲他們想要的類型嗎?」此時,它會查看目標對象,並發現當前被引用爲Product的對象實際上是一個已上傳的CartItem。因此,將其重新轉換回原來的CartItem是安全的。再一次,重點在於,一旦您將其創建爲CartItem,它始終是一個CartItem,無論您將它放置或放下多少次。但是,在您提供的示例中,您的產品絕不僅僅是一個簡單的產品。它從來不是CartItem開始,也不能鑄造。

+0

所以你說沒有辦法我可以實現我的自定義按鈕類,因爲鑄造只是平坦不會工作? – tetris11

+0

不,他是說沒有辦法將Button投射到gButton上。 ;)嘗試在佈局中指定gButtons而不是按鈕。有兩種方法可以做到這一點:完全限定XML元素中的包名稱,例如:或使用您將需要標準View構造函數接受參數'(Context,AttributeSet)'以便在XML佈局中使用它。 – adamp

2

要投按鈕gButton應聲明按鈕這樣的:

<com.example.gButton 
    android:text="Page 3" 
    android:id="@+id/Button03" 
    android:layout_width="250px" 
    android:textSize="18px" 
    android:layout_height="55px"> 
</com.example.gButton> 

,但你應該把你包,而不是「com。示例」

而且你gButton類必須有下一個構造函數:

package com.example; 

public class gButton extends Button{ 
    ... 

    public gButton(Context context, AttributeSet attrs) { // this constructor will be called by inflater for creating object instance 
     super(context, attrs); 
    } 

    ... 

}

然後你就可以施展你的按鈕,你在你的問題中寫道。但請注意,您的構造函數將不會被稱爲

+0

謝謝,非常有用和明確的答案。明天再確認答案之前我會試試這個。 – tetris11

+0

如果是,請將其標記爲答案。 – NOSTRA