2

Im trying to move from one screen to the other using buttons, i CAN move from main to secondary but when trying to get back from the second screen i get an error message "unfortunately, app has stopped".

Note: I will have a 3rd layout/activity so i will copy the solution to this 3rd option.

Im new in android and wonder if you can provide a better approach to what im doing (activities ARE declared in manifest, actually when using the 2nd screen as main, it goes FINE to the 1st screen (as 2nd option) BUT when trying to get back to 2nd screen it gave me the error again), thx in advance!!:

package com.example.citas.medicas; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.LinearLayout; import android.widget.TextView; import android.widget.Toast; public class Citas_Medicas extends Activity { private Button btnIraRegistrarPaciente; private Button btnIraRegistrarDoctor; private Button btnIraRegistrarCita; private Button btnIraReportePacientes; private Button btnIraReporteHistorialCitas; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_citas__medicas); btnIraRegistrarPaciente = (Button)findViewById(R.id.btnIraRegistrarPaciente); btnIraRegistrarDoctor = (Button)findViewById(R.id.btnIraRegistrarDoctor); btnIraRegistrarCita = (Button)findViewById(R.id.btnIraRegistrarCita); btnIraReportePacientes = (Button)findViewById(R.id.btnIraReportePacientes); btnIraReporteHistorialCitas = (Button)findViewById(R.id.btnIraReporteHistorialCitas); } public void onStart() { super.onStart(); btnIraRegistrarPaciente.setOnClickListener(new OnClickListener() { public void onClick(View component) { setContentView(R.layout.registrarpaciente); } } ); } 

}

Here is the secondary java (not sure if the onStart is fine):

package com.example.citas.medicas; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.LinearLayout; import android.widget.TextView; import android.widget.Toast; public class Registrar_Paciente extends Activity implements OnClickListener { private Button btnRegistrarPaciente; private Button btnVolverMenuPrincipal1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.registrarpaciente); btnRegistrarPaciente = (Button)findViewById(R.id.btnRegistrarPaciente); btnVolverMenuPrincipal1 = (Button)findViewById(R.id.btnVolverMenuPrincipal1); btnRegistrarPaciente.setOnClickListener(this); btnVolverMenuPrincipal1.setOnClickListener(this); } public void onStart() { super.onStart(); btnRegistrarPaciente.setOnClickListener( new OnClickListener() { public void onClick(View component) { setContentView(R.layout.registrarpaciente); } } ); btnVolverMenuPrincipal1.setOnClickListener( new OnClickListener() { public void onClick(View component) { setContentView(R.layout.activity_citas__medicas); //Intent intent = new Intent(Registrar_Paciente.this, Citas_Medicas.class); //startActivity(intent); } } ); } } 
1
  • Could you please provide the LogCat when your app gets force closed ? Commented Sep 21, 2012 at 9:12

2 Answers 2

2

Instead of putting the onClickHandlers inside onStart(), I believe it would be better if you put them inside onCreate() itself. I mean, that is the conventional way of doing it.

Since you are using different activities, merely changing the setContentView() would not be sufficient. I might be wrong too, but the standard way to switch between activities is via Intents.

Something like this :

btnIraRegistrarPaciente.setOnClickListener(new OnClickListener() { public void onClick(View component) { Intent intent = new Intent(context, Registrar_Paciente.class); startActivity(intent); } } ); 
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you @swayam!!. For some reason at android curse it was recommended to include the onClickHandlers inside onStart(), fyi.
Oh, okay. I myself have never used it. It is totally okay to put them in onCreate(). Good luck ahead! :)
1

You may need change activity instead change layout in Activity. Take care of function SetContentView b/c it will release the memory allocated for the control in the layout you already set before.

In First Activity:

@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_citas__medicas); btnIraRegistrarPaciente = (Button)findViewById(R.id.btnIraRegistrarPaciente); btnIraRegistrarPaciente.setOnClickListener(new OnClickListener() { public void onClick(View component) { //setContentView(R.layout.registrarpaciente); Intent intent = new Intent(Citas_Medicas.this, Registrar_Paciente.class); startActivity(intent); } } ); } 

In Second Activity:

@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.registrarpaciente); btnRegistrarPaciente = (Button)findViewById(R.id.btnRegistrarPaciente); btnRegistrarPaciente.setOnClickListener(new OnClickListener() { public void onClick(View component) { //setContentView(R.layout.registrarpaciente); Intent intent = new Intent(Registrar_Paciente.this, Citas_Medicas.class); startActivity(intent); } } ); } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.