Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

In your main Activity, in the findViewbyId() method, you pass the ID of your Button which you declared in you xml.

So your button code is :-

<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" />

here , id is button_main

so in you Class, you need to do

 final Button button = (Button) findViewById(R.id.button_main);//Button ID goes here
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top