my application has two text views ( TV1 and TV2) and a button. When you tap the button Show a random number in TV2. I want the sum of random numbers in the value TV2 I have in the TV1 and so on. But it fails and the app closes, can you say me what Im doing wrong ? Thank you
Here is my code:
public class MainActivity extends Activity {
Button b1;
TextView tv1, tv2;
Random myRandom;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv1 = (TextView) findViewById(R.id.tv1);
b1 = (Button) findViewById(R.id.b1);
tv2=(TextView)findViewById(R.id.tv2);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void a(View view){
String result = "";
myRandom = new Random();
for (int i = 0; i < 1; i++) {
result += String.valueOf(myRandom.nextInt(100)) + "\n";
}
tv2.setText("Lo vendiste por: " + result);
String x=tv1.getText().toString();
String y=tv2.getText().toString();
int nro1=Integer.parseInt(x);
int nro2=Integer.parseInt(y);
int sumar = nro1+nro2;
String resultado=String.valueOf(sumar);
tv1.setText(resultado);
}
}