I'm new and I'm starting with Android in Android Studio .. I have a simple application that has two activities, the 1st asks for and sends a name and the second shows it, but when the second one is launched it does not show the name, the application is closed without showing the name ..
Main Activity
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
private EditText txtNombre;
private Button btnAceptar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Obtenemos una referencia a los controles de la interfaz
txtNombre = (EditText)findViewById(R.id.txtNombre);
btnAceptar = (Button)findViewById(R.id.btnAceptar);
//Implementamos el evento click del botón
btnAceptar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Creamos el Intent
Intent intent =
new Intent(MainActivity.this, SaludoActivity.class);
//Creamos la información a pasar entre actividades
Bundle b = new Bundle();
b.putString("NOMBRE", txtNombre.getText().toString());
//Añadimos la información al intent
intent.putExtras(b);
//Iniciamos la nueva actividad
startActivity(intent);
}
});
}
}
Actity greeting (2nd activity)
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class SaludoActivity extends AppCompatActivity {
private TextView txtSaludo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_saludo);
//Localizar los controles
txtSaludo = (TextView)findViewById(R.id.txtSaludo);
//Recuperamos la información pasada en el intent
Bundle bundle = this.getIntent().getExtras();
//Construimos el mensaje a mostrar
txtSaludo.setText("Hola " + bundle.getString("NOMBRE"));
}
}
What can I do so that the app does not close when I press the OK button? .. To test the app I am using bluestack, I do not know if it will have something to do .. Thanks!