Button constraint issues i believe

54 Views Asked by At

Hope the weekend is going well. I am working on login and registration pages and having a hard time finding a solution to an unchecked cast error and after checking, it seemed everything is fine on the xml code. the challenge is really frustrating, Here is the code please

java code

public class RegistrationPage < Button > extends AppCompatActivity {
    EditText emailId, password;
    Button btnSignin;
    TextView tvSign_Up;
    FirebaseAuth mFirebaseAuth;

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

            mFirebaseAuth = FirebaseAuth.getInstance();
            emailId = findViewById(R.id.editTextTextEmailAddress);
            password = findViewById(R.id.editTextTextPassword);
            btnSignin = (Button) findViewById(R.id.Sign_In);
            tvSign_Up = (findViewById(R.id.textView2));
            btnSignin.(new RegistrationPage < > () View.OnClickListener(("v"))) - > {
                    @Override
                    public void onClick(View "V") {
                        string email = emailId.getText().toString();
                        string pwd = password.getText().toString();
                        if (((String) email).isEmpty()) {
                            emailId.setError("Please Enter Correct Email Address");
                            emailId.requestFocus();
                        }

The xml code

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".RegistrationPage">

<EditText
android:id="@+id/editTextTextEmailAddress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="84dp"
android:layout_marginLeft="84dp"
android:layout_marginTop="152dp"
android:layout_marginEnd="85dp"
android:layout_marginRight="85dp"
android:layout_marginBottom="313dp"
android:autofillHints=""
android:ems="10"
android:hint="@string/email"
android:inputType="textEmailAddress"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/editTextTextPassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="85dp"
android:layout_marginLeft="85dp"
android:layout_marginEnd="85dp"
android:layout_marginRight="85dp"
android:autofillHints=""
android:ems="10"
android:hint="@string/password"
android:inputType="textPassword"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editTextTextEmailAddress"
app:layout_constraintVertical_bias="0.092" />

<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="29dp"
android:layout_marginBottom="200dp"
android:text="@string/already_have_an_account_sign_in_here"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editTextTextPassword"
app:layout_constraintVertical_bias="0.677" />
<Button
android:id="@+id/Sign_In"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:layout_marginStart="50dp"
android:layout_marginLeft="50dp"
android:layout_marginTop="24dp"
android:layout_marginEnd="50dp"
android:layout_marginRight="50dp"
android:text="@string/sign_up"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editTextTextPassword"
app:layout_constraintVertical_bias="0.054" />

</androidx.constraintlayout.widget.ConstraintLayout>

Any help will be appreciated greatly

2

There are 2 best solutions below

0
On

Seems like there is some coding format errors. Replace your above java code with this and try it out:

mFirebaseAuth = FirebaseAuth.getInstance();
emailId = findViewById(R.id.editTextTextEmailAddress);
password = findViewById(R.id.editTextTextPassword);
btnSignin = findViewById(R.id.Sign_In);
tvSign_Up = findViewById(R.id.textView2);

btnSignin.setOnClickListener(v -> {
    String email = emailId.getText().toString();
    String pwd = password.getText().toString();
    if (email.isEmpty()) {
        emailId.setError("Please Enter Correct Email Address");
        emailId.requestFocus();
    }
});
0
On

This is not an error per se, it's a compile-time warning, sometimes warnings are irrelevant, sometimes they're not.

An alternative, not solution is to suppress that warning using

@SuppressWarnings ("unchecked") 

or

@Suppress("unchecked")

Before

btnSignin = findViewById(R.id.Sign_In); // remove (Button), is not necesary

SuppressWarning

@SuppressWarnings instruct the compiler to ignore or suppress the specified compiler warning in annotated element and all program elements inside that element, as an example, if a class is annotated to suppress a particular warning, then a warning generated during the methodology inside that class will also be suppressed.

@SuppressWarnings("unchecked") and @SuppressWarnings("serial") are two of most popular samples of @SuppressWarnings annotation. Also if you limit the scope, this way it won’t even affect the entire method.

SuppressWarnings indicates the potential programming error or mistake, so it's recommended to use them slenderly and instead attempt to solve actual problem which is triggering that warning. But there are situations when you are absolutely sure that things won't go wrong, you can use @SuppressWarnings to suppress those warnings.