0

This is my code in Android Studio of building a register Activity for an Android app. I am getting these class Cast Exception errors every time I run my app. What could be the problem?

My RegisterActivity.java code:

{
    private TextInputLayout inputEmail,inputPassword,inputConfirmPassword;
    Button btnRegister;
    TextView alreadyHaveAccount;
    FirebaseAuth mAuth;
    ProgressDialog mLoadingBar;

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

        inputEmail = findViewById(R.id.inputEmail);
        inputPassword = findViewById(R.id.inputPassword);
        inputConfirmPassword = findViewById(R.id.inputConfirmPassword);
        alreadyHaveAccount = findViewById(R.id.alreadyHaveAccount);
        mAuth = FirebaseAuth.getInstance();
        mLoadingBar = new ProgressDialog(this);

        btnRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AttemptRegistration();
            }
        });

        alreadyHaveAccount.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(RegisterActivity.this,LoginActivity.class);
                startActivity(intent);
            }
        });
    }

    private void AttemptRegistration() {
        String email = inputEmail.getEditText().getText().toString();
        String password = inputPassword.getEditText().getText().toString();
        String confirmPassword= inputConfirmPassword.getEditText().getText().toString();

        if (email.isEmpty() || !email.contains("@gmail.com"))
        {
            showError(inputEmail,"Email is not valid");

        }
        else if(password.isEmpty() || password.length()<5)
        {
            showError(inputPassword,"Password must be greater than 5 letters ");
        }
        else if(!confirmPassword.equals (password))
        {
            showError(inputConfirmPassword,"Password does not match");
        }
        else
        {
            mLoadingBar.setTitle("Registration");
            mLoadingBar.setMessage("Please Wait, While we Check Your Credentials");
            mLoadingBar.setCanceledOnTouchOutside(false);
            mLoadingBar.show();
            mAuth.createUserWithEmailAndPassword(email,password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull @org.jetbrains.annotations.NotNull Task<AuthResult> task) {
                    if(task.isSuccessful())
                    {
                        mLoadingBar.dismiss();
                        Toast.makeText(RegisterActivity.this, "Registration Successful", Toast.LENGTH_SHORT).show();
                        Intent intent = new Intent (RegisterActivity.this,SetupActivity.class);
                        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK);
                        startActivity(intent);
                        finish();
                    }
                    else
                    {
                        mLoadingBar.dismiss();
                        Toast.makeText(RegisterActivity.this, "Registration Failed", Toast.LENGTH_SHORT).show();
                    }
                }
            });
        }
    }

    private void showError(TextInputLayout field, String text) {

        field.setError(text);
        field.requestFocus();
    }
}

The xml file which is activity_register.xml for the same is as follows.

<?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"
    android:background="@mipmap/background"
    tools:context=".RegisterActivity">

    <TextView
        android:id="@+id/logo"
        android:layout_width="169dp"
        android:layout_height="51dp"
        android:fontFamily="serif"
        android:text=" REGISTER"
        android:textSize="30dp"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.207" />

    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/inputEmail"
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="150dp"
        android:layout_marginTop="60dp"
        android:layout_marginEnd="150dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/logo"
        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox">

        <com.google.android.material.textfield.TextInputEditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="textEmailAddress"
            android:hint="E-MAIL" />
    </com.google.android.material.textfield.TextInputLayout>

    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/inputPassword"
        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="150dp"
        android:layout_marginTop="20dp"
        android:layout_marginEnd="150dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/inputEmail">

        <com.google.android.material.textfield.TextInputEditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="PASSWORD"
            android:inputType="textPassword" />
    </com.google.android.material.textfield.TextInputLayout>

    <Button
        android:id="@+id/btnRegister"
        android:layout_width="134dp"
        android:layout_height="47dp"
        android:layout_marginTop="35dp"
        android:text="REGISTER"
        android:textAllCaps="false"
        app:backgroundTint="#00BCD4"
        app:layout_constraintEnd_toEndOf="@+id/inputPassword"
        app:layout_constraintStart_toStartOf="@+id/inputPassword"
        app:layout_constraintTop_toBottomOf="@+id/textInputLayout" />

    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/textInputLayout"
        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="150dp"
        android:layout_marginTop="20dp"
        android:layout_marginEnd="150dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/inputPassword">

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/inputConfirmPassword"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="CONFIRM PASSWORD" />
    </com.google.android.material.textfield.TextInputLayout>

    <TextView
        android:id="@+id/alreadyHaveAccount"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:text="ALREADY HAVE AN ACCOUNT?"
        android:textSize="12sp"
        app:layout_constraintEnd_toEndOf="@+id/textInputLayout"
        app:layout_constraintStart_toStartOf="@+id/textInputLayout"
        app:layout_constraintTop_toBottomOf="@+id/btnRegister" />

</androidx.constraintlayout.widget.ConstraintLayout>

On running this register activity I am getting the following error in my LogCat:

Caused by: java.lang.ClassCastException: com.google.android.material.textfield.TextInputEditText cannot be cast to com.google.android.material.textfield.TextInputLayout
    at com.example.meetup.RegisterActivity.onCreate(RegisterActivity.java:36)
    at android.app.Activity.performCreate(Activity.java:8000)
    at android.app.Activity.performCreate(Activity.java:7984)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3422)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:223)
    at android.app.ActivityThread.main(ActivityThread.java:7656)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)

This is my build.gradle file:

plugins {
    id 'com.android.application'
    id 'com.google.gms.google-services'
}

android {
    compileSdkVersion 30
    buildToolsVersion "30.0.3"

    defaultConfig {
        applicationId "com.example.meetup"
        minSdkVersion 19
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

dependencies {
    implementation 'androidx.appcompat:appcompat:1.3.0'
    implementation 'com.google.android.material:material:1.4.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
    implementation 'com.google.firebase:firebase-auth:21.0.1'
    implementation 'com.google.firebase:firebase-database:20.0.0'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
    implementation 'com.google.android.material:material:1.4.0'
    implementation 'org.jetbrains:annotations:16.0.2'

    /*
        implementation 'com.google.firebase:firebase-auth:21.0.1'
        implementation 'com.google.firebase:firebase-core:19.0.0'
        implementation 'com.google.firebase:firebase-storage:20.0.0'
        implementation 'com.firebaseui:firebase-ui-database:6.2.1'
    */
}
Akshat kant
  • 125
  • 6
  • Look where you've set `android:id="@+id/inputConfirmPassword"` in your layout. – Mike M. Jul 17 '21 at 20:03
  • The first of part of the code seems to be missing something (the class declaration). It can't be the complete content of file *RegisterActivity.java*. – Peter Mortensen Aug 01 '21 at 15:24
  • @PeterMortensen I reverted your title edit that changed an error message. "cannot be cast to" is language from the error message, so it should be left intact for searching. – Ryan M Aug 03 '21 at 09:55
  • This is 100% not a duplicate of the question linked. What is wrong with people? – blimpse Jan 28 '22 at 20:36

1 Answers1

1

The issue is here.

In your layout inputConfirmPassword is a TextInputEditText

<com.google.android.material.textfield.TextInputLayout
    android:id="@+id/textInputLayout"..>

    <com.google.android.material.textfield.TextInputEditText
        android:id="@+id/inputConfirmPassword"

while in your code inputConfirmPassword is a TextInputLayout :

private TextInputLayout inputEmail,inputPassword,inputConfirmPassword;


@Override
protected void onCreate(Bundle savedInstanceState) {
    //..
    inputConfirmPassword = findViewById(R.id.inputConfirmPassword);
Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841