Android: NullPointerException in .getIntent (). getExtras ()

3

I'm in an android class project and I have it almost finished but I need to pass some variables from one activity to another, it gives me NullPointerException and looking for I have not found anything.

  

Logcat

     

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Bundle android.content.Intent.getExtras ()' on a null object reference                                                                              at com.grupo3.cebancburger.AnadirHamburguesa. (AddHamburguesa.java:31)

The intent that I do in class.

MainActivity:

(The views)

nomb = (EditText)findViewById(R.id.nombre);
dire = (EditText)findViewById(R.id.direccion);
tele = (EditText)findViewById(R.id.telefono);
email = (EditText)findViewById(R.id.email);

(Intent)

intent = new Intent(DatosCliente.this, AnadirHamburguesa. 
intent.putExtra("nombre", nomb.getText().toString());
intent.putExtra("direccion", dire.getText().toString());
intent.putExtra("telefono", tele.getText().toString());
intent.putExtra("email", email.getText().toString());
startActivity(intent);

Add Hamburg (the getIntent)

Bundle extras = getIntent().getExtras();
String nom = extras.getString("nombre");
String dire = extras.getString("direccion");
String tele = extras.getString("telefono");
String email = extras.getString("email");
    
asked by Aritzbn 11.01.2018 в 19:32
source

1 answer

5

Try to retrieve the information in your activity Add Hamburg without using Bundle, as follows

Add Hamburg

Intent intent = getIntent();
String nombre = intent.getStringExtra("nombre");
String dire = intent.getStringExtra("direccion");    
...

If you use Bundle to pass data you must create the Bundle and pass it in the intent

Bundle bundle = new Bundle();
Intent intent = new Intent(DatosCliente.this, AnadirHamburguesa.class)
bundle.putString("nombre",nombre);
intent.putExtras(bundle);
startActivity(intent);

In Adding Hamburg

Bundle extras = getIntent().getExtras();
String nom = extras.getString("nombre");

In the future try to use Parcelable or Serializable to pass objects from one activity to another. For example, if you have a Client-type object that implements the Parcelable class

Cliente cliente = new Cliente(nombre,direccion);

Intent intent = new Intent(getApplicationContext(, SigActividad.class);
intent.putExtra("cliente", cliente);
startActivity(intent);

In SigActivity:

Cliente cliente = getIntent().getExtras().getParcelable("nombre");
...
    
answered by 11.01.2018 / 19:39
source