4
respostas

App fecha ao clicar em botão

Olá, tudo bem? Eu criei um app de cadastro de pessoas no Android Studio e estou tendo alguns problemas... Quando clico no botão de cadastro, ao invés de listar o cadastro realizado o app fecha...

Segue meu MainActivity, só possuo essa atividade rodando

MainActivity:

package com.example.cadastro;

public class MainActivity extends AppCompatActivity {

    private EditText nome, idade, email;
    private RadioButton masculino, feminino, intersexo, prefiroNaoDizer;
    private TextView textNome, textIdade, textEmail, textSexo;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        nome = findViewById(R.id.editNome);
        idade = findViewById(R.id.editIdade);
        email = findViewById(R.id.editEmail);

        masculino = findViewById(R.id.radioButtonMasc);
        feminino = findViewById(R.id.radioButtonFem);
        intersexo = findViewById(R.id.radioButtonInt);
        prefiroNaoDizer = findViewById(R.id.radioButtonNao);

        textNome = findViewById(R.id.textNome);
        textIdade = findViewById(R.id.textIdade);
        textEmail = findViewById(R.id.textEmail);
        textSexo = findViewById(R.id.textSexo);

        Button botaoCadastro = findViewById(R.id.btCadastrar);

        botaoCadastro.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                String Nome = nome.getText().toString();
                String Idade = idade.getText().toString();
                String Email = email.getText().toString();

                if (Nome.isEmpty() || Idade.isEmpty() || Email.isEmpty()){
                    Snackbar.make(view, "Não deixe de preencher todos os campos :)", Snackbar.LENGTH_SHORT).show();
                } else {
                    btSelecionado(view);
                }
            }
        });
    }
    private void btSelecionado(View view) {
        if(masculino.isChecked()) {
            textNome.setText((CharSequence) textNome);
            textIdade.setText((CharSequence) textIdade);
            textEmail.setText((CharSequence) textEmail);
            textSexo.setText("Sexo: Masculino");
        }else if(feminino.isChecked()) {
            textNome.setText((CharSequence) textNome);
            textIdade.setText((CharSequence) textIdade);
            textEmail.setText((CharSequence) textEmail);
            textSexo.setText("Sexo: Feminino");
        }else if(intersexo.isChecked()) {
            textNome.setText((CharSequence) textNome);
            textIdade.setText((CharSequence) textIdade);
            textEmail.setText((CharSequence) textEmail);
            textSexo.setText("Sexo: Intersexo");
        }else if(prefiroNaoDizer.isChecked()) {
            textNome.setText((CharSequence) textNome);
            textIdade.setText((CharSequence) textIdade);
            textEmail.setText((CharSequence) textEmail);
            textSexo.setText("Sexo: Prefiro não dizer");
        }else{
            Snackbar.make(view, "Lembre de marcar o seu sexo ;)", Snackbar.LENGTH_SHORT).show();
        }
    }
}
4 respostas

Esse é meu layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:fontFamily="sans-serif"
        android:gravity="center"
        android:text="@string/cadastro_de_pessoas"
        android:textColor="#01385C"
        android:textSize="34sp"
        android:textStyle="bold"
        tools:layout_editor_absoluteX="34dp"
        tools:layout_editor_absoluteY="32dp" />

    <EditText
        android:id="@+id/editNome"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:ems="10"
        android:hint="Nome Completo"
        android:inputType="textPersonName"
        android:maxLength="45"
        android:minHeight="48dp" />

    <EditText
        android:id="@+id/editIdade"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:ems="10"
        android:hint="Idade"
        android:inputType="number"
        android:maxLength="2"
        android:minHeight="48dp" />

    <EditText
        android:id="@+id/editEmail"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:ems="10"
        android:hint="Email@email.com.br"
        android:inputType="textEmailAddress"
        android:minHeight="48dp" />

    <TextView
        android:id="@+id/TextView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="30dp"
        android:layout_marginTop="15dp"
        android:text="Sexo:"
        android:textColor="#000000"
        android:textSize="20sp"
        android:textStyle="bold" />

    <RadioGroup
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp">

        <RadioButton
            android:id="@+id/radioButtonMasc"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Masculino"
            android:textSize="16sp" />

        <RadioButton
            android:id="@+id/radioButtonFem"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Feminino"
            android:textSize="16sp" />

        <RadioButton
            android:id="@+id/radioButtonInt"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Intersexo"
            android:textSize="16sp" />

        <RadioButton
            android:id="@+id/radioButtonNao"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Prefiro não dizer"
            android:textSize="16sp" />

    </RadioGroup>

    <Button
        android:id="@+id/btCadastrar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="30dp"
        android:layout_marginRight="30dp"
        android:text="Cadastrar"/>

    <TextView
        android:id="@+id/textNome"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="15dp"
        android:textSize="18sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/textIdade"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="5dp"
        android:textSize="18sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/textEmail"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="5dp"
        android:textSize="18sp"
        android:textStyle="bold" />

// código limitado para servir aqui

</LinearLayout>

E esse é o AndroidManifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.cadastro">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.Cadastro">
        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

E esse é o erro que aparece no Logcat:

2022-01-14 13:11:52.155 19182-19182/com.example.cadastro D/AndroidRuntime: Shutting down VM
2022-01-14 13:11:52.204 19182-19182/com.example.cadastro E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.cadastro, PID: 19182
    java.lang.ClassCastException: com.google.android.material.textview.MaterialTextView cannot be cast to java.lang.CharSequence
        at com.example.cadastro.MainActivity.btSelecionado(MainActivity.java:61)
        at com.example.cadastro.MainActivity.access$300(MainActivity.java:15)
        at com.example.cadastro.MainActivity$1.onClick(MainActivity.java:54)
        at android.view.View.performClick(View.java:7448)
        at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1131)
        at android.view.View.performClickInternal(View.java:7425)
        at android.view.View.access$3600(View.java:810)
        at android.view.View$PerformClick.run(View.java:28305)
        at android.os.Handler.handleCallback(Handler.java:938)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        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)
2022-01-14 13:11:52.289 19182-19182/com.example.cadastro I/Process: Sending signal. PID: 19182 SIG: 9

Essas linhas estão com erro:

  textNome.setText((CharSequence) textNome);
  textIdade.setText((CharSequence) textIdade);
 textEmail.setText((CharSequence) textEmail);

Porque são campos de view, ui, componentes, e você esta tentando transformar-los em charsequence, no caso você vai precisar fazer a extração do campo, igual faz acima pegando o getText e jogando para string