Textinputedittext cannot be converted

Asked

Viewed 208 times

-1

This here is my Activity class.

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.EditText;

import com.google.android.material.textfield.TextInputLayout;

public class FormularioCadastroActivity extends AppCompatActivity {

    private TextInputLayout textInputNomeCompleto;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_formulario_cadastro);
        textInputNomeCompleto = (TextInputLayout) findViewById(R.id.formulario_cadastro_campo_nome_completo);
        EditText campoNomeCompleto = textInputNomeCompleto.getEditText();
        String nomeCompleto = campoNomeCompleto.getText().toString();
        if(nomeCompleto.isEmpty()){
            campoNomeCompleto.setError("Campo Obrigatório");
        }
    }
}

The corresponding XML code:

<com.google.android.material.textfield.TextInputEditText
    android:id="@+id/formulario_cadastro_campo_nome_completo"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginEnd="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="20dp"
    android:hint="Nome Completo"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/textView"/>

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

    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/formulario_cadastro_campo_nome_completo">

</com.google.android.material.textfield.TextInputLayout>

This is the error message I receive:

 Caused by: java.lang.ClassCastException: com.google.android.material.textfield.TextInputEditText cannot be cast to com.google.android.material.textfield.TextInputLayout

I am also providing the Gradle file in case there is something wrong with the version of material.

apply plugin: 'com.android.application'

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.2"

    defaultConfig {
        applicationId "com.qintess.cadastroqintess.ui.cadastroqintess"
        minSdkVersion 26
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

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

}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])

    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
    implementation 'com.google.android.material:material:1.1.0'

}

So, does anyone have any idea how to fix this exception?

1 answer

1


In XML, your element with id formulario_cadastro_campo_nome_completo is an element TextInputEditText.

In Java code, you are trying to put it into a variable of type TextInputLayout, so the error is about not being able to convert the types.

To tidy up, just leave the two (XML and Java) with the same type according to your need.

Example:

Java

private TextInputLayout textInputLayoutNomeCompleto;
private TextInputEditText textInputEditNomeCompleto;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    textInputLayoutNomeCompleto = findViewById(R.id.textInputLayout);
    textInputEditNomeCompleto = findViewById(R.id.formulario_cadastro_campo_nome_completo);
}

It is not necessary to leave the casting explicit in findViewById, because when you store in a variable a View returned is already converted to variable type.

It is also possible to store this element in a type variable EditText for the TextInputEditText inherits EditText. If you want to use some more specific method of the Material component, then you need to use the most specific class :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.