android - TextInputEditText 无法转换为 TextInputLayout

标签 android view android-edittext

我的 Activity 中有这段代码

public class RegisterActivity extends AppCompatActivity {


private static final String TAG = RegisterActivity.class.getSimpleName();
private TextInputLayout mDisplayName;
private TextInputLayout mEmail;
private TextInputLayout mPassword;
private Button mCreateBtn;

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

    mAuth = FirebaseAuth.getInstance();

    mDisplayName = (TextInputLayout)findViewById(R.id.reg_display_name);
    mEmail = (TextInputLayout)findViewById(R.id.reg_email);
    mPassword = (TextInputLayout)findViewById(R.id.reg_password);
    mCreateBtn = (Button) findViewById(R.id.reg_create_btn);

    mCreateBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String displayName = mDisplayName.getEditText().getText().toString();
            String email = mEmail.getEditText().getText().toString();
            String password = mPassword.getEditText().getText().toString();

            registerUser(displayName,email,password);

        }


    });
}

private void registerUser(String displayName, String email, String password) {

   mAuth.signInWithEmailAndPassword(email,password)
           .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
               @Override
               public void onComplete(@NonNull Task<AuthResult> task) {
                     if(task.isSuccessful()){
                         Intent mainIntent = new Intent(RegisterActivity.this,MainActivity.class);
                         startActivity(mainIntent);
                         finish();
                     }else{
                         Toast.makeText(RegisterActivity.this,"You got some error.",Toast.LENGTH_SHORT).show();
                     }
               }
           });

}


}

其对应的XML代码为:

<android.support.constraint.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=".RegisterActivity">
    
    <android.support.design.widget.TextInputLayout
        android:id="@+id/textInputLayout2"
        android:layout_width="368dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">
        
        <android.support.design.widget.TextInputEditText
            android:id="@+id/reg_display_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="hint"
            android:text="Display name" />
    </android.support.design.widget.TextInputLayout>
    
    <android.support.design.widget.TextInputLayout
        android:id="@+id/textInputLayout3"
        android:layout_width="368dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textInputLayout2">

        <android.support.design.widget.TextInputEditText
            android:id="@+id/reg_email"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="hint"
            android:text="Email" />
    </android.support.design.widget.TextInputLayout>
    
    <android.support.design.widget.TextInputLayout
        android:id="@+id/textInputLayout4"
        android:layout_width="368dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textInputLayout3">
    
        <android.support.design.widget.TextInputEditText
            android:id="@+id/reg_password"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="hint"
            android:text="Password" />
    </android.support.design.widget.TextInputLayout>

    <Button
        android:id="@+id/reg_create_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:text="Create account"
        android:textAllCaps="false"
        app:layout_constraintTop_toBottomOf="@+id/textInputLayout4"
        tools:layout_editor_absoluteX="255dp" />
</android.support.constraint.ConstraintLayout>

当我运行应用程序时,我收到了这条消息:

Caused by: java.lang.ClassCastException: android.support.design.widget.TextInputEditText cannot be cast to android.support.design.widget.TextInputLayout

我也给你 gradle 文件以防万一设计版本有问题。

 apply plugin: 'com.android.application'

 android {
 compileSdkVersion 27
 defaultConfig {
    applicationId "theo.tziomakas.lapitchat"
    minSdkVersion 21
    targetSdkVersion 27
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner 
    "android.support.test.runner.AndroidJUnitRunner"
  }
   buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
   }
}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support:design:27.1.1'
implementation 'com.android.support.constraint:constraint- 
layout:1.1.0'
implementation 'com.google.firebase:firebase-auth:11.6.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 
'com.android.support.test.espresso:espresso-core:3.0.2'
 }

 apply plugin: 'com.google.gms.google-services'

那么有什么想法可以解决这个异常吗?

谢谢 西奥。

最佳答案

尝试转换为

private TextInputEditText mDisplayName;
private TextInputEditText mEmail;
private TextInputEditText mPassword;


mDisplayName = (TextInputEditText)findViewById(R.id.reg_display_name);
mEmail = (TextInputEditText)findViewById(R.id.reg_email);
mPassword = (TextInputEditText)findViewById(R.id.reg_password);

关于android - TextInputEditText 无法转换为 TextInputLayout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50150450/

相关文章:

android - 三星上的资源$NotFoundException

Android: 有没有人有webview screen fit 的好方法?

android - 如何将日期和时间添加到android中的textview

android - ListPreferences 的异常(exception)情况

javascript - ExtJS 4 : rendering a custom view from a grouped store with a title for the grouping

android - 更改EditText android中的键盘输入文本

ruby-on-rails - rails 形式的隐藏字段

python - Django错误: create_user() takes at least 2 arguments (3 given)

java - 为什么 Android 不检索我输入的文本?

android - 仅使用 XML 更新 Android EditText 对焦点更改的提示