简体   繁体   English

Android 应用程序中的“TextInputEditText 无法转换为 TextInputLayout”

[英]“TextInputEditText cannot be cast to TextInputLayout” in Android app

This is my code in Android Studio of building a register Activity for an app.这是我在 Android Studio 中为应用程序构建注册活动的代码。 I am getting these classCastException errors every time I run my app.每次运行我的应用程序时,我都会收到这些 classCastException 错误。 What is the problem?问题是什么?

My RegisterActivity.java code:我的 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();
}
}

The xml file which is activity_register.xml for the same is as follows.相同的 xml 文件是 activity_register.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>

On running this register activity I am getting the following error in my logcat:在运行此注册活动时,我的 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这是我的 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'*/
}

The issue is here.问题就在这里。

In your layout inputConfirmPassword is a TextInputEditText在你的布局inputConfirmPassword是一个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 :而在您的代码中inputConfirmPasswordTextInputLayout

private TextInputLayout inputEmail,inputPassword,inputConfirmPassword;


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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么 TextInputEditText 在 Android Studio 中无法转换为 TextInputLayout? - Why TextInputEditText cannot be cast to TextInputLayout in Android Studio? Android 材质 TextInputLayout 和 TextInputEditText - Android Material TextInputLayout and TextInputEditText Android TextInputEditText/TextInputLayout 切断错误信息 - Android TextInputEditText/TextInputLayout cuts off error message android.view.InflateException:二进制 XML 文件第 27 行:android.support.design.widget.TextInputEditText 不能转换为 android.view.ViewGroup - android.view.InflateException: Binary XML file line #27: android.support.design.widget.TextInputEditText cannot be cast to android.view.ViewGroup 如何从 TextInputLayout 或 TextInputEditText 中删除下划线 - How can remove underline from TextInputLayout or TextInputEditText android.app.Application无法转换为MainActivity - android.app.Application cannot be cast to MainActivity MainActivity 不能强制转换为 android.app.Activity - MainActivity cannot be cast to android.app.Activity 无法强制转换为android.app.DatePickerDialog $ OnDateSetListener - cannot be cast to android.app.DatePickerDialog$OnDateSetListener 服务无法转换为android.app.Activity - service cannot be cast to android.app.Activity android.app.Application 无法转换为 android.app.Activity - android.app.Application cannot be cast to android.app.Activity
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM