Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android- Cannot Cast from View to Button

Okay, I'm new to both java and android programming, but I've come across the most frustrating error ever and I can't seem to be able to find the solution. here is where the error is at.

final Button button = (Button) findViewById(R.id.activity_button);

And here is the XML of the button itself.

<Button
    android:id="@+id/button_main"
    android:layout_width="match_parent"
    android:layout_height="90dp"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:text="@string/button_text" />

I'm not seeing the problem, I imported import com.awesometech.uselessbutton.R; which didn't help. Any suggestions? I'm using Eclipse.

like image 605
user3013656 Avatar asked Mar 19 '26 17:03

user3013656


1 Answers

Change to

 final Button button = (Button) findViewById(R.id.button_main);

You have

<Button
android:id="@+id/button_main" // id is button_main not activity_button

Probably activity_button id belongs to another view and you are casting it to a Button.

like image 77
Raghunandan Avatar answered Mar 22 '26 07:03

Raghunandan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!