Switch from second to third activity [Android Studio]

0

How do I change from the second to the third screen ??

I was able to switch from screen1 to screen2 (from Main to CadastroActivity ) Except that, using the same code, I can not get from screen 2 to 3 (from CadastroActivity to FormularioActivity ). It compiles, but when I click the button, the app gives error and quits

How do I solve it?

I want to click the btn_cadastrar (from CadastroActivity ) button and go to FormularioActivity

Java code:

package com.example.vanessa.projetoinicial_vanessa;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class CadastroActivity extends AppCompatActivity {

//private Button botaoCadastrar; //da tela Cadastro

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


    //Ao clicar no botão Cadastrar (da tela 2 Cadastro), deve ir para tela Formulario
    Button botaoCadastrar = (Button) findViewById(R.id.btn_cadastrar);
    botaoCadastrar.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(CadastroActivity.this, FormularioActivity.class);
            startActivity(intent);
        }
    });

}

}

XML:     

<LinearLayout
    android:layout_width="368dp"
    android:layout_height="624dp"
    android:orientation="vertical"
    tools:layout_editor_absoluteX="8dp"
    tools:layout_editor_absoluteY="8dp">

    <ListView
        android:id="@+id/lista_Madicamentos"
        android:layout_width="match_parent"
        android:layout_height="200dp" />

    <Button
        android:id="@+id/btn_cadastrar"
        style="?attr/buttonStyle"
        android:layout_width="match_parent"
        android:layout_height="39dp"
        android:background="?attr/colorPrimary"
        android:text="CADASTRAR"
        tools:layout_editor_absoluteX="16dp"
        tools:layout_editor_absoluteY="528dp" />

</LinearLayout>

</RelativeLayout>

In LogCat:

10-13 01:09:24.954 17082-17082/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.vanessa.projetoinicial_vanessa, PID: 17082
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.vanessa.projetoinicial_vanessa/com.example.vanessa.projetoinicial_vanessa.FormularioActivity}: java.lang.ClassCastException: android.support.v7.widget.AppCompatTextView cannot be cast to android.widget.EditText
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
    at android.app.ActivityThread.-wrap11(Unknown Source:0)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6494)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
 Caused by: java.lang.ClassCastException: android.support.v7.widget.AppCompatTextView cannot be cast to android.widget.EditText
    at com.example.vanessa.projetoinicial_vanessa.FormularioActivity.onCreate(FormularioActivity.java:25)
    at android.app.Activity.performCreate(Activity.java:7009)
    at android.app.Activity.performCreate(Activity.java:7000)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856) 
    at android.app.ActivityThread.-wrap11(Unknown Source:0) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589) 
    at android.os.Handler.dispatchMessage(Handler.java:106) 
    at android.os.Looper.loop(Looper.java:164) 
    at android.app.ActivityThread.main(ActivityThread.java:6494) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) 

Where is the error? Thank you so much.

    
asked by anonymous 13.10.2018 / 02:10

1 answer

1

In the log it says:

AppCompatTextView cannot be cast to EditText

That is, the error is on the next screen. You are trying to save an object of type AppCompatTextView to a variable of type EditText (probably with the findViewById method), and this is causing an error in your code.

    
13.10.2018 / 03:52