I'm having problem using my custom Button class.
I've tried changing the xml layout from <Button/> to <gButton/>, but that hasn't worked.
I've also tried casting Button page1 = (gButton) findView...., in Line 15 but for some reason that doesn't work (you can cast a child class, right?)
The error I keep getting from LogCat is:
Caused by: java.lang.ClassCastException: android.widget.Button
at flash.tut.ButtonPage.onCreate(ButtonPage.java:15)
Any help is much appreciated.
Here's my button view:
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);
}
});
}
}
and my gButton which extends the button class:
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);
}
}
And finally my xml layout:
<?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>