kmb
kmb

Reputation: 73

receving Class Cast Exception caused by ImageButton

I have this app which was working find and suddenly is crashing caused by ClassCastException: android.widget.ImageButton. The only change I made between working and not was the background to the relative layout from a solid color to a drawable (blue with a pattern).

xml file:

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

<include
    android:id="@+id/linearLayout1_ref"
    android:layout_width="match_parent"
    android:layout_height="75dip"
    layout="@layout/title_area" />


<RelativeLayout
    android:id="@+id/relativeLayout1"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/fur" >

    <View
        android:id="@+id/view5"
        style="@style/BlueBandHor"
        android:layout_width="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />

    <View
        android:id="@+id/view3"
        style="@style/BlueBandVer"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@id/view5"
        android:layout_centerVertical="true" />

    <View
        android:id="@+id/view4"
        style="@style/BlueBandVer"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_below="@id/view5"
        android:layout_centerVertical="true" />

    <View
        android:id="@+id/view1"
        style="@style/BlueBandHor"
        android:layout_width="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="48dp" />

    <ImageButton
        android:id="@+id/btn1"
        style="@style/Image"
        android:layout_below="@id/view5"
        android:layout_centerHorizontal="true"
        android:contentDescription="@string/portrait"
        android:onClick="onButtonClick"
        android:paddingLeft="5dip"
        android:paddingRight="5dip"
        android:src="@drawable/orionhead" 
        android:background="@drawable/button_active" 
        android:clickable="true"/>

    <ImageButton
        android:id="@+id/btn3"
        style="@style/Image"
        android:layout_toRightOf="@+id/view3"
        android:contentDescription="@string/portrait"
        android:onClick="onButtonClick"
        android:paddingLeft="5dip"
        android:paddingRight="5dip"
        android:src="@drawable/stonewallhead" 
        android:background="@drawable/button_active" 
        android:clickable="true" 
        android:layout_centerVertical="true"/>

    <Button
        android:id="@+id/how_tobtn"
        style="@style/HowTo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/view1"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="20dip"
        android:background="@drawable/howto_button_active"
        android:padding="5dip"
        android:text="@string/How_to"
        android:textColor="@color/yellow"
        android:textSize="25dip" 
        android:onClick="onButtonClick"/>

    <ImageButton
        android:id="@+id/btn2"
        style="@style/Image"
        android:layout_toLeftOf="@+id/view4"
        android:contentDescription="@string/portrait"
        android:onClick="onButtonClick"
        android:paddingLeft="5dip"
        android:paddingRight="5dip"
        android:src="@drawable/truffelhead" 
        android:background="@drawable/button_active" 
        android:clickable="true" 
        android:layout_centerVertical="true"/>

</RelativeLayout>

java file:

 public class ImageActivity extends Activity {

 @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

public void onClick(View view) 
{Intent intent;
    switch (view.getId()) {
    case R.id.how_tobtn:
        intent = new Intent(this, Howbtn.class);
        break;
    case R.id.btn1:
        intent = new Intent(this, Btn1.class);
        break;
    case R.id.btn2:
        intent = new Intent(this, Btn2.class);
        break;
    default:
    intent = new Intent(this, Btn3.class);
    break;
            }   

Any ideas of what the problem is? Thank you for any help.

Upvotes: 1

Views: 1267

Answers (1)

Orkun
Orkun

Reputation: 7238

I think the problem is that somewhere in your code, you are treating the how_tobtn as an ImageButton whereas it s actually a normal Button.

<Button
        android:id="@+id/how_tobtn"
        style="@style/HowTo"
        android:layout_width="wrap_content"
...

The error is probably in some line like:

(ImageButton)getView(R.id.how_tobtn);

To avoid this, either change your xml or cast it to the correct type in the code:

(Button)getView(R.id.how_tobtn);

EDIT:

I d also reconsider the naming of my Activities. Class names like Btn, HowtoBtn can be confusing, especially for yourself.

Upvotes: 1

Related Questions