为什么 "TextInputEditText cannot be cast to TextInputLayout" 可以在 Android Studio 中?

Why can "TextInputEditText cannot be cast to TextInputLayout" in Android Studio?

这是我在 Android Studio 中为 Android 应用构建寄存器 Activity 的代码。每次我 运行 我的应用程序时,我都会收到这些 class Cast Exception 错误。可能是什么问题?

我的RegisterActivity.java代码:

{
    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();
    }
}

同activity_register.xml的xml文件如下

<?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>

在 运行 注册这个寄存器 activity 我在 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)

这是我的build.gradle文件:

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'
    */
}

问题就在这里。

在您的布局中 inputConfirmPasswordTextInputEditText

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

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

而在您的代码中 inputConfirmPasswordTextInputLayout :

private TextInputLayout inputEmail,inputPassword,inputConfirmPassword;


@Override
protected void onCreate(Bundle savedInstanceState) {
    //..
    inputConfirmPassword = findViewById(R.id.inputConfirmPassword);