Im pretty much a newbie at this so i could use some help to know why does my app keep crashing ? I’ve tried a lot of ways and this is the best I’ve come so far.
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView lastAnswer = (TextView) findViewById(R.id.textView5);
final TextView answerLabel = (TextView) findViewById(R.id.textView1);
final TextView answerLabel1 = (TextView) findViewById(R.id.textView2);
final TextView answerLabel2 = (TextView) findViewById(R.id.textView3);
final EditText enteredNumber = (EditText) findViewById(R.id.editText1);
Button getAnswerButton = (Button) findViewById(R.id.button1);
getAnswerButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Random randomGenerator = new Random();
//rand gene
int randomNumber2 = randomGenerator.nextInt(99);
int randomNumber = randomGenerator.nextInt(99);
String number = "";
String number2 = "";
String jtext = " Times ";
number2= Integer.toString(randomNumber2);
number= Integer.toString(randomNumber);
answerLabel.setText(jtext);
answerLabel1.setText(number2);
answerLabel2.setText(number);
String content = enteredNumber.getText().toString();
When i comment the conditional the app works fine, but thats not all that i want
I suppose the problem is in the conditional but i can’t seem to find it.
no errors what so ever on eclipse.
if (content != null){
int anInt=Integer.parseInt(content);
int result= randomNumber2 *randomNumber;
if(anInt == result ){
Context context =getApplicationContext();
CharSequence text = "That's correct!!";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
} else{
Context context = getApplicationContext();
CharSequence text = "That's not correct";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
lastAnswer.setText(content);
}
}
});}
So what im trying to do here (mind the noob code) is just generate 2 random numbers, display them and then have the user input the result, compare it, notify if right or not and then regenerate.. I’ve been working on this for days
any help is appreciated, even new ways of following this code. thanks.
You should probably surround your parsing with a try/catch. Users input being parsed back into a number might not always work – especially with EditTexts. Change
So it is
Also to make life easier, you should make the EditText accept in only numbers.