Switch from second to third Activity [Android Studio]

Asked

Viewed 210 times

0

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

I managed to switch from tela1 to tela2 (from Main for CadastroActivity) But using the same code, I can’t get from screen 2 to 3 (from CadastroActivity for FormularioActivity). It compiles, but when I click the button, the app gives error and ends

How do I resolve?

I want to click the button btn_cadastrar (of CadastroActivity) 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 very much

  • What error does it make? This is always the first step to take. Put the error message in the question.

  • Gives error already in the running app. It goes from first to second screen. On the second screen, when clicking on the button, appears the message "Projectneil Vanessa has stopped" "Open app Again"

  • You have at your disposal the application log. There you have everything about the error. Take it and put in your question.

  • All right, that’s it?

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 store an Appcompattextview object in an Edittext type variable (probably with the findViewById method), and this is causing an error in your code.

  • Ahhh, thank you very much. It really is on the next screen... I commented here and it worked, now I will fix it. //I started learning now, I did not know about this error screen rs, thanks

Browser other questions tagged

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