Pass values between Activity AndroidStudio

0

I want to pass two values from one Activity to another, this is my code

MainActivity

public class MainActivity extends AppCompatActivity implements View.OnClickListener {


EditText ip_intro;

Button botonPlantaBaja;
Button botonPlanta0;

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    botonPlantaBaja = (Button) findViewById(R.id.boton_pb);
    botonPlantaBaja.setOnClickListener(this);

    botonPlanta0 = (Button) findViewById(R.id.boton_p0);
    botonPlanta0.setOnClickListener(this);
}

@Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.boton_pb:
            String datobaja= ip_intro.getText().toString();
            int p_baja=-1;
            Intent intent_baja = new Intent(MainActivity.this,SecondActivity.class);
            intent_baja.putExtra("DATO",datobaja);
            intent_baja.putExtra("PLANTA",p_baja);
            startActivity(intent_baja);
            break;
        case R.id.boton_p0:
            String dato0= ip_intro.getText().toString();
            int p_0=0;
            Intent intent_0 = new Intent(MainActivity.this,SecondActivity.class);
            intent_0.putExtra("DATO",dato0);
            intent_0.putExtra("PLANTA",p_0);
            startActivity(intent_0);
            break;

And the class where I want to pass the values

SecondActivity

public class SecondActivity extends AppCompatActivity implements 
SensorEventListener {

private TextView texto_ip;
private TextView texto_planta;
private String dato_ip;
int num_planta;


    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_second);

    texto_ip= (TextView)findViewById(R.id.texto_ip);
    texto_planta= (TextView)findViewById(R.id.texto_planta);


    Intent intent = getIntent();
    Bundle datos= intent.getExtras();

    dato_ip= datos.getString("DATO");
    texto_ip.setText(dato_ip);
    num_planta=datos.getInt("PLANTA");
    texto_planta.setText("Estamos en la planta: "+num_planta);
}

Could someone help me, and tell me where the fault is? And if you could put a code I would appreciate it very much

Greetings

    
asked by Antonio 09.03.2018 в 12:54
source

1 answer

2

You have forgotten the findViewById of ip_intro in the onCreate of the MainActivity.

EditText ip_intro = (EditText) findViewById(R.id.aquiPonTuVista);

Without this when you do String datobaja= ip_intro.getText().toString(); you will fail because you have not linked the edittext view to the variable.

    
answered by 09.03.2018 / 13:17
source