Sending data on Android - can not be referenced by a static context


In putExtra I get the following error cannot be referenced by a static context and I do not understand why, I just want to pass the data to the 2nd screen

public class MainActivity extends AppCompatActivity {

protected void onCreate(Bundle savedInstanceState) {

    final EditText dato1 = (EditText) findViewById(;
    final EditText dato2 = (EditText) findViewById(;
    final Button boton_enviar = (Button) findViewById(;
    final RadioButton rb1 = (RadioButton) findViewById(;
    final RadioButton rb2 = (RadioButton) findViewById(;
    final RadioButton rb3 = (RadioButton) findViewById(;
    final RadioButton rb4 = (RadioButton) findViewById(;

    boton_enviar.setOnClickListener(new View.OnClickListener() {

        String v1 = dato1.getText().toString();
        String v2 = dato2.getText().toString();

        int operador1 = Integer.parseInt(v1);
        int operador2 = Integer.parseInt(v2);
        final int suma = operador1 + operador2;
        final double resta = operador1 - operador2;
        final double multiplicacion = operador1 * operador2;
        final double division = operador1 / operador2;

        public void onClick(View v) {

                   switch(v.getId()) {
                           Intent intent = new Intent(MainActivity.this,Main2Activity.class);

                           if (rb1.isSelected()) {

                           } else if (rb2.isChecked()) {

                           } else if (rb3.isChecked()) {

                           } else if (rb4.isChecked()) {



asked by J.newbie 01.12.2018 в 14:12

1 answer


You are referencing the class instead of the instance.

Change Intent.putExtra("suma",suma); by intent.putExtra("suma",suma);

On the other hand, the onClickListener is adding it to boton_enviar , so the View passed to onClick() is always boton_enviar . You do not need the switch to know what View is about.

answered by 01.12.2018 / 15:12