2

Hi I am getting this message android.widget.imagebutton cannot be cast to android.widget.button (See attachment pic for logcat error) which I don't understand why? because my code seems to be right.

Code:

ImageButton Calendar = (ImageButton) findViewById (R.id.Calendar);
Calendar.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

        Intent OpenCalendarbtn = new Intent (Menu.this, DisplayCalendar.class);
        startActivity (OpenCalendarbtn);
    }
});

enter image description here

Complete Layout xml file:

`

<ImageButton
    android:id="@+id/Read"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:layout_alignParentRight="true"
    android:layout_alignTop="@+id/Write"
    android:layout_marginRight="31dp"
    android:src="@drawable/read" />

<ImageView
    android:id="@+id/AppLogo"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:src="@drawable/logo" />

<ImageButton
    android:id="@+id/Write"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/AppLogo"
    android:layout_marginLeft="35dp"
    android:layout_marginTop="14dp"
    android:src="@drawable/write" />

<ImageButton
    android:id="@+id/Calendar"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:layout_alignLeft="@+id/Write"
    android:layout_below="@+id/Write"
    android:layout_marginTop="20dp"
    android:src="@drawable/calendar/>

`

3
  • Is R.id.Calendar an ImageButton or a Button? Commented Mar 15, 2014 at 14:06
  • please add your layout.xml Commented Mar 15, 2014 at 14:08
  • R.id.Calendar is an ImageButton. Commented Mar 15, 2014 at 14:45

3 Answers 3

2

Make sure you have used the <ImageButton> tag instead of <Button> tag in your layout resource file. That could be the cause of the error.

Sign up to request clarification or add additional context in comments.

3 Comments

Layout resource file looks ok as well <ImageButton android:id="@+id/Calendar" android:layout_width="100dp" android:layout_height="100dp" android:layout_below="@+id/Read" android:layout_centerHorizontal="true" android:layout_marginTop="21dp" android:src="@drawable/calendar" />
post your complete layout xml
added the full layout xml
0

Change your

ImageButton Calendar = (ImageButton) findViewById (R.id.Calendar); 

To:

Button Calendar = (Button) findViewById (R.id.Calendar); 

Comments

0

Its an Eclipse problem not in your code. Solution for this is just copy your code in xml by Ctrl+c and save the xml then again paste it again then save it and run.....this works with me.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.