Passing parameters from an Activity to a calculation class

Asked

Viewed 313 times

0

I would like you to help me in a situation that I am doing in my project, but that somehow the app to by clicking on the calculate button.

I’ll give you a simple example to get a sense of more or less what I need. The user enters two numbers in a data entry activity, these numbers go to a class (only to calculate, without Activity) and then return to a result activity.

I need you to show me exactly how this transition is done, what I missed, etc. This is because in my project I have many more variables and many more accounts to be done, and I will need two classes of calculations (because the calculations are different)

Thank you for your attention. I hope I have been clear and given a simple example for solution.

Activity of data entry

    public class Entrada_Dados extends AppCompatActivity  implements View.OnClickListener{

    //Declarando variáveis
        public EditText txt_numero1;
        public EditText txt_numero2;

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

    //Relacionando variáveis com objetos
        txt_numero1=(EditText)findViewById(R.id.txt_numero1);
        txt_numero2= EditText)findViewById(R.id.txt_numero2);

    }

    public void onClick (View View) {

    //Transferindo parâmetros
        Intent it = new Intent(this, Calculo.class );

        it.putExtra("numero1", txt_numero1.getText().toString());
        it.putExtra("numero2", txt_numero2.getText().toString());

        startActivity(it);
        finish();
    }
}

Calculation class

 public class Calculo extends AppCompatActivity {

    //Declarando variáveis
    public double numero1;
    public double numero2;
    public double resultado;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        String txt_numero1 = getIntent().getStringExtra("numero1"); //Recuperar na string
        numero1=Double.parseDouble(txt_numero1); //Passando para double

        String txt_numero2 = getIntent().getStringExtra("numero2"); //Recuperar na string
        numero2=Double.parseDouble(txt_numero2); //Passando para double

        startActivity(new Intent(this, Resultado.class));

    }

public double calculo_soma(){

        return resultado = numero1 + numero2;

    }

Result activity

  public class Resultado extends AppCompatActivity implements View.OnClickListener{

    public TextView txt_resultado;

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

        txt_resultado= (TextView)findViewById(R.id.txt_resultado);

    }

    public void onClick (View v){

    //Chamando a classe
        Calculo Calculo = new Calculo ();

        double calculo_soma = Calculo.calculo_soma(); 
        String stg_resultado = Double.toString(calculo_soma);
        txt_resultado.setText(stg_resultado);

    }
}

**They asked what error I was making (remembering that the example I gave here is not that of my project, but the figure I’m going to add is the app I’m making ** Erro

  • What is the error in logcat?

  • I put the error image in the post. I do not know how to interpret these errors yet :( I apologize.

  • You have a button where in xml is declared a onClick that does not have the respective method in Activity.

  • Dude... it’s weird because on this very button is a code where he checks to see if Edittext’s are empty, and it works. Now, when all Edittext are filled in, it should take the information to a calculation class and then open an Activity with the answers. Then he doesn’t do.

  • It may be that my parameter transfer reasoning is inconsistent. I deduced that this may be what is crashing the app.

  • And another interesting fact that I notice is that in the buttons that you have in the project, when I go to the Onclick event, in the properties, to select which public void the button has to act, two identical events always appear. Type, I click on Onclick and two options appear, for example: "Onclick (Result)" and "Onclick (Result)". When this occurs I select the second one, because it is what works, the first one does not work rsrsrs

  • It seems the error that happens to me when I declare in xml a more forgotten method to put in java code

  • But in my case it is stated in both java and xml code.

  • Guys, is my reasoning above correct? rsrsrs

  • Instead of creating classes for the calculations, why don’t you create methods and keep everything in the same Ctivity? Or create an activity for each type of calculation, but keep the calculations attached to the corresponding activity. Much easier to pass the data and maintain a cohesive code.

Show 5 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.