Question

I have one error sending one variable from one activity to other. In the first activity I have Inicio, Fin and Numero and when I click in the button it starts other activity and I print into TextView this values. For debbuging I comment my code and I try to print only one of this values(inicio). This is the code I have in the first activity.

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_examenes);
    final EditText txtTemaIn=(EditText)findViewById(R.id.txtTemaIn);
    final EditText txtTemaFin=(EditText)findViewById(R.id.txtTemaFin);
    final EditText txtNumPregs=(EditText)findViewById(R.id.txtNumPregs);
    final Button btnVerExam=(Button)findViewById(R.id.btnVerExam);
    btnVerExam.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
         Intent intent = new Intent(Examenes.this, Examen.class);
         Bundle b=new Bundle();
         b.putString("Inicio", txtTemaIn.getText().toString());
         b.putString("Fin", txtTemaFin.getText().toString());
         b.putString("Numero", txtNumPregs.getText().toString());
         startActivity(intent);
        }
    });
}

In the second activity I have this code.

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_examen);
    Bundle bundle = this.getIntent().getExtras();
            txtTemaExa=(TextView)findViewById(R.id.txtTemaExa);
            txtTemaExa.setText(bundle.getString("Inicio"));
}
Was it helpful?

Solution

I think you forget to put Bundle in Intent like

intent.putExtras(b);

Correct with below:

 Intent intent = new Intent(Examenes.this, Examen.class);
 Bundle b=new Bundle();
 b.putString("Inicio", txtTemaIn.getText().toString());
 b.putString("Fin", txtTemaFin.getText().toString());
 b.putString("Numero", txtNumPregs.getText().toString());
 intent.putExtras(b);
 startActivity(intent);

OTHER TIPS

please find below code

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_examenes);
final EditText txtTemaIn=(EditText)findViewById(R.id.txtTemaIn);
final EditText txtTemaFin=(EditText)findViewById(R.id.txtTemaFin);
final EditText txtNumPregs=(EditText)findViewById(R.id.txtNumPregs);
final Button btnVerExam=(Button)findViewById(R.id.btnVerExam);
btnVerExam.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
     Intent intent = new Intent(Examenes.this, Examen.class);
     Bundle b=new Bundle();
     b.putString("Inicio", txtTemaIn.getText().toString());
     b.putString("Fin", txtTemaFin.getText().toString());
     b.putString("Numero", txtNumPregs.getText().toString());
intent.putExtras(b);// here is change 

     startActivity(intent);
    }
});
}

even easier , you are not obliged to creat the bundle, the extra of the intent is a bundle:

 Intent intent = new Intent(Examenes.this, Examen.class);
    intent.putExtra("Inicio",txtTemaIn.getText().toString());
    intent.putExtra("Fin",txtTemaFin.getText().toString());
    intent.putExtra("Numero",txtNumPregs.getText().toString());        

     startActivity(intent);

then to get the extras you can get it like this:

final Bundle extras = getIntent().getExtras();
txtTemaExa.setText(extras.getString("Inicio"));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top