代码之家  ›  专栏  ›  技术社区  ›  Theo

无法将TextInputText转换为TextInputLayout

  •  5
  • Theo  · 技术社区  · 6 年前

    我的活动中有此代码

    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'
    

    那么,有没有办法解决这个异常呢?

    谢谢 西奥。

    7 回复  |  直到 3 年前
        1
  •  9
  •   Tung Tran    5 年前

    尝试转换为

    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);
    
        2
  •  1
  •   Cao Minh Vu    6 年前

    尝试以下操作:

    public class RegisterActivity extends AppCompatActivity {
    
    
    private static final String TAG = RegisterActivity.class.getSimpleName();
    private TextInputEditText mDisplayName;
    private TextInputEditText mEmail;
    private TextInputEditText 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 = (TextInputEditText)findViewById(R.id.reg_display_name);
        mEmail = (TextInputEditText)findViewById(R.id.reg_email);
        mPassword = (TextInputEditText)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();
                         }
                   }
               });
    
    }
    
    
    }
    
        3
  •  1
  •   Karthik    6 年前

    您正在尝试将TextInputItText id与java文件中的TextInputLayout类绑定。确保使用相同类型的小部件和类名绑定id。

    在java文件中,使用TextInputItText而不是TextInputLayout声明变量

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

    在casting中,将TextInputLayout转换为TextInputItText

    mDisplayName = (TextInputEditText)findViewById(R.id.reg_display_name);
    mEmail = (TextInputEditText)findViewById(R.id.reg_email);
    mPassword = (TextInputEditText)findViewById(R.id.reg_password);
    
        4
  •  1
  •   Josh Lea Acquah    3 年前

    为创建另一个id TextInputEditText 把它作为你的参考

    <com.google.android.material.textfield.TextInputLayout
      style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
            android:id="@+id/textEmail"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="100dp"
            app:boxStrokeColor="@color/purple_700"
            app:boxCornerRadiusTopEnd="100dp"
            app:boxCornerRadiusBottomStart="100dp"
            app:boxCornerRadiusBottomEnd="100dp"
            app:boxCornerRadiusTopStart="100dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">
            <com.google.android.material.textfield.TextInputEditText
                android:id="@+id/textEditEmail"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="@string/email" />
    </com.google.android.material.textfield.TextInputLayout>
    TextInputEditText textEmail = (TextInputEditText)findViewById(R.id.textEditEmail);
    
        5
  •  0
  •   Zerman Isakov    5 年前

    在更改TextInputLayouyt的所有ID后,我修复了该问题。

        6
  •  0
  •   Babar Ali    3 年前

    我采用了同样的方法,更改了所有字段的所有ID,问题得到了解决

        7
  •  0
  •   Awais Qarni    3 年前

    剪切ID表单TextInputItemText并粘贴到TextInputLayout中

    <com.google.android.material.textfield.TextInputLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
            android:hint="Username"
            android:textColorHint="@color/black"
            app:boxStrokeColor="@color/black"
            app:boxStrokeWidthFocused="2dp"
            app:endIconTint="@color/black"
            app:hintTextColor="@color/black"
            app:endIconMode="clear_text"
            app:startIconTint="@color/black"
            android:id="@+id/signup_username_editText"
            >
    
            <com.google.android.material.textfield.TextInputEditText
               
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:inputType="text"
                android:textColor="@color/black"
                android:textCursorDrawable="@null" />
        </com.google.android.material.textfield.TextInputLayout>