Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error using findViewById method

I'm trying to use the findViewById() method to to assign an action to a button I created, however it is giving me the error:

findViewById(int) in android.support.v7.app.AppCompatActivity cannot be applied to (android.widget.Button)

at the lines:

buttonStudentAccess = (Button) findViewById(buttonStudentAccess);
buttonGuestAccess = (Button) findViewById(buttonGuestAccess);

My code is as follows:

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

public class UserTypeSelection extends AppCompatActivity implements View.OnClickListener{

private Button buttonStudentAccess;
private Button buttonGuestAccess;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_user_type_selection);

    buttonStudentAccess = (Button) findViewById(buttonStudentAccess);
    buttonGuestAccess = (Button) findViewById(buttonGuestAccess);

    buttonStudentAccess.setOnClickListener(this);
    buttonGuestAccess.setOnClickListener(this);
}

@Override
public void onClick(View view) {
    if (view == buttonStudentAccess) {
}
    if (view == buttonGuestAccess) {
        //startActivity(new Intent(this, MainActivity.class));
    }
}

The corresponding xml file contains the buttons

<Button
    android:text="Guest"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/buttonStudentAccess"
    android:layout_alignLeft="@+id/buttonStudentAccess"
    android:layout_alignStart="@+id/buttonStudentAccess"
    android:layout_marginTop="30dp"
    android:id="@+id/buttonGuestAccess"
    android:layout_alignRight="@+id/buttonStudentAccess"
    android:layout_alignEnd="@+id/buttonStudentAccess" />

<Button
    android:text="Student"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="30dp"
    android:id="@+id/buttonStudentAccess"
    android:layout_below="@+id/textView2"
    android:layout_centerHorizontal="true" />

I used the same syntax to create and assign actions to buttons in my login and registration classes and it didn't create this error. Any help is appreciated, thank you

like image 318
Spartan123 Avatar asked Mar 03 '26 16:03

Spartan123


2 Answers

You need to specify where you're getting the id "buttonStudentAccess" from. so here's how you can find the view:

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

To specify that the button or any other view is located in the "resources" package you refer to it as R and .id specifies that you want to grab the view using its id.

like image 111
Aria Pahlavan Avatar answered Mar 06 '26 07:03

Aria Pahlavan


buttonStudentAccess = (Button) findViewById(R.id.buttonStudentAccess);
buttonGuestAccess = (Button) findViewById(R.id.buttonGuestAccess);
like image 21
Aleksandar G Avatar answered Mar 06 '26 05:03

Aleksandar G



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!