0

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);
    }
}
Frederick Eskens
  • 386
  • 3
  • 20
Agustin Val
  • 187
  • 7
  • What is the the value being returned here? `String x=tv1.getText().toString();`. Also please post your logcat when the app closes. – Daniel Jun 03 '16 at 14:38

1 Answers1

0

You are trying to parse a word to an Integer:

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);

So the String x is Lo vendiste por: "random number" and you want to parse this into a int, thats not possible

It should be something like:

tv2.setText(result);

String x=tv1.getText().toString();
String y=tv2.getText().toString();

int nro1=Integer.parseInt(x);
int nro2=Integer.parseInt(y);

You could also try splitting the string:

tv2.setText("Lo vendiste por: " + result);


String x=tv1.getText().toString();
String y=tv2.getText().toString();

String[] splittedString = x.split(" ");

int nro1=Integer.parseInt(splittedString[2]);
int nro2=Integer.parseInt(y);

I can't prove if this works correctly, I didnt try the code, but for splitting a string here is a sample too

Community
  • 1
  • 1
rib
  • 190
  • 1
  • 11