Spartan123
Spartan123

Reputation: 103

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

Upvotes: 3

Views: 1119

Answers (2)

Aria Pahlavan
Aria Pahlavan

Reputation: 1418

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.

Upvotes: 4

Aleksandar G
Aleksandar G

Reputation: 1181

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

Upvotes: 2

Related Questions