User44 User44 - 1 month ago 7
Android Question

What is causing my app to crash in this program? - Android Studio

I have written a program where random numbers are generated and displayed on a screen, being shown they are added together. The user then has to enter the sum of the two or more numbers to score point. It is all made clear in the code shown below. My problem is that when the button to start the game is presses, my app crashes/ I didn't have this issue before, though the program wasn't fully complete then so I had to develop it further. In this development I have encountered the issue of my application crashing. I believe this has something to do with the loadG2a and loadG2b random int variables, and possibly either their integration into parseInt as (numbers.getText()) or setting them together in the 'numbers' TextView in first place. It may not be these at all. Either way, I have attempted to solve this solution but have failed to find a fix. Here's the code:

final Button loseStarter2;

loseStarter2 = (Button) findViewById(R.id.Starter2);
loseStarter2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
loseStarter2.setVisibility(View.GONE);
infoG2.setVisibility(View.GONE);

final Random generateG2 = new Random();
final int loadG2a = generateG2.nextInt(89)+10;
final int loadG2b = generateG2.nextInt(89)+10;
final int loadG2c = 0;
final int loadG2d = 0;
final TextView numbers = (TextView) findViewById(R.id.numbers);
numbers.setText(loadG2a+loadG2b);
final EditText input2 = (EditText) findViewById(R.id.input2);
if (true) {
input2.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
switch (keyCode) {
case KeyEvent.KEYCODE_ENTER:
Editable answer2 = input2.getText();
int finalAnswer2 = Integer.parseInt(String.valueOf(answer2));
int finalLoadG2 = Integer.parseInt(String.valueOf(numbers.getText()));
input2.getText().clear();

if (finalAnswer2 == finalLoadG2) {
score++;
}


if (score>=0 && score<=3){
int loadG2a = generateG2.nextInt(89)+10;
int loadG2b = generateG2.nextInt(89)+10;
int loadG2c = 0;
int loadG2d = 0;
numbers.setText(loadG2a+loadG2b);

}

if (score>=4 && score<=6){
int loadG2a = generateG2.nextInt(899)+100;
int loadG2b = generateG2.nextInt(899)+100;
int loadG2c = 0;
int loadG2d = 0;
numbers.setText(loadG2a+loadG2b);
}

if (score>=7 && score<=9){
int loadG2a = generateG2.nextInt(89)+10;
int loadG2b = generateG2.nextInt(89)+10;
int loadG2c = generateG2.nextInt(89)+10;
int loadG2d = 0;
numbers.setText(loadG2a+loadG2b+loadG2c);
}

if (score>=10 && score<=12){
int loadG2a = generateG2.nextInt(899)+100;
int loadG2b = generateG2.nextInt(899)+100;
int loadG2c = generateG2.nextInt(899)+100;
int loadG2d = 0;
numbers.setText(loadG2a+loadG2b+loadG2c);
}

if (score>=13 && score<=15){
int loadG2a = generateG2.nextInt(89)+10;
int loadG2b = generateG2.nextInt(89)+10;
int loadG2c = generateG2.nextInt(89)+10;
int loadG2d = generateG2.nextInt(89)+10;
numbers.setText(loadG2a+loadG2b+loadG2c+loadG2d);
}

if (score>=16 && score<=18){
int loadG2a = generateG2.nextInt(899)+100;
int loadG2b = generateG2.nextInt(899)+100;
int loadG2c = generateG2.nextInt(899)+100;
int loadG2d = generateG2.nextInt(899)+100;
numbers.setText(loadG2a+loadG2b+loadG2c+loadG2d);
}

if (score>=19 && score<=20){
int loadG2a = generateG2.nextInt(8999)+1000;
int loadG2b = generateG2.nextInt(8999)+1000;
int loadG2c = generateG2.nextInt(8999)+1000;
int loadG2d = 0;
numbers.setText(loadG2a+loadG2b+loadG2c);
}

if (score>=21 && score<=22){
int loadG2a = generateG2.nextInt(8999)+1000;
int loadG2b = generateG2.nextInt(8999)+1000;
int loadG2c = generateG2.nextInt(8999)+1000;
int loadG2d = generateG2.nextInt(8999)+1000;
numbers.setText(loadG2a+loadG2b+loadG2c+loadG2d);
}

if (score>=23){
int loadG2a = generateG2.nextInt(89999)+10000;
int loadG2b = generateG2.nextInt(89999)+10000;
int loadG2c = generateG2.nextInt(89999)+10000;
int loadG2d = generateG2.nextInt(89999)+10000;
numbers.setText(loadG2a+loadG2b+loadG2c+loadG2d);
}


}
}

return false;
}
});
}


I'd appreciate anyone willing to help me resolve this issue, and get the program running in the way intended. Please feel free to substitute any code necessary. Many thanks in advance.

Answer
// here is int's (all other lines with int's must be also corrected)
numbers.setText(loadG2a+loadG2b+loadG2c+loadG2d); 

// but must be String =)
numbers.setText(loadG2a+loadG2b+loadG2c+loadG2d+"");
Comments