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