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

0

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 {


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

    final EditText dato1 = (EditText) findViewById(R.id.editText);
    final EditText dato2 = (EditText) findViewById(R.id.editText2);
    final Button boton_enviar = (Button) findViewById(R.id.boton);
    final RadioButton rb1 = (RadioButton) findViewById(R.id.checkBox);
    final RadioButton rb2 = (RadioButton) findViewById(R.id.checkBox2);
    final RadioButton rb3 = (RadioButton) findViewById(R.id.checkBox3);
    final RadioButton rb4 = (RadioButton) findViewById(R.id.checkBox4);



    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()) {
                       case R.id.boton:
                           Intent intent = new Intent(MainActivity.this,Main2Activity.class);
                           Intent.putExtra("suma",suma);

                           if (rb1.isSelected()) {


                           } else if (rb2.isChecked()) {


                           } else if (rb3.isChecked()) {


                           } else if (rb4.isChecked()) {


                           }
                           startActivity(intent);
                   }


            }
    });



}
}
    
asked by J.newbie 01.12.2018 в 14:12
source

1 answer

0

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
source