0
I cannot pass a textview value from an Activity A to Activity B to perform some calculations. I tried through the putExtra method to pass variable of type string and then convert to double and could not and already tried also by Bundle and also could not, I can only pass variables of type string independent of which method I use, but I can not convert it. And when sure pass the variables the button to perform the calculations does not work and closes the app. What should I do to be able to pass the variable and make the calculation through the button in Activity B?
Follow the string passing code by Bundle.
excerpt from the Activity A code where the button calls Activity B:
public void padiola (View v){
Intent telapad = new Intent(this,Padiola.class);
Bundle parametros = new Bundle();
String va = vua.getText().toString();
String vb = vub.getText().toString();
parametros.putString("chavea", va);
parametros.putString("chaveb", vb);
telapad.putExtras(parametros);
startActivity(telapad);
}
Activity B:
TextView qtda, qtdb, alta, altb, vuar, vubr;
@SuppressLint("SetTextI18n")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_padiola);
qtda = (TextView) findViewById(R.id.qtdPadareia);
qtdb = (TextView) findViewById(R.id.qtdPadbrita);
alta = (TextView) findViewById(R.id.altAreia);
altb = (TextView) findViewById(R.id.altBrita);
vuar = (TextView) findViewById(R.id.novoVA);
vubr = (TextView) findViewById(R.id.novoVB);
Intent telapad = getIntent();
Bundle parametros = telapad.getExtras();
String va1 = parametros.getString("chavea");
String vb1 = parametros.getString("chaveb");
vuar.setText(va1);
vubr.setText(vb1);
}
public void trintaxquarenta(View v) {
//Para Areia//
double s1 = Double.parseDouble(vuar.getText().toString());
double s2 = Double.parseDouble(vubr.getText().toString());
double s3 = s1 + s2;
qtda.setText(Double.toString(s3));
}
}
I don’t see anything wrong with the code. What do you mean by "I can’t"? It makes a mistake?
– ramaral
The code described above is string passthrough, the code is right. But I need a variable type double to be able to do the calculations and I am not able to convert the string to double, because when I click on the button to perform the sum it of the error and close the app.
– IF12
Then it is because the text in Edittext is not valid as Double. See if the problem is not exchange point for comma, or vise-versa.
– ramaral
But the values are not in Edittext, they are stored in Text View. Like so point or comma ?
– IF12
Put the error that gives.
– ramaral
The mistake is that the App has stopped working, just This.
– IF12
After vse commented on and went to research and identified that the conversion problem was that the string was comma and was not converting to double. I got it through the replace method.
– IF12