I want to output a random number that is always even in a Activity! here is my code, where is the problem?
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fourth);
OnClickButtonListener();
textBox.setText("" + randomInt);
}
double randNumber = Math.random();
int d = (int) (randNumber * 100 );
int randomInt = (int)d * 2;
TextView textBox = (TextView) findViewById(R.id.textView8);
You need to initialize your TextView inside your onCreate() method. Your code would look something like this
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fourth);
TextView textBox = (TextView) findViewById(R.id.textView8);
double randNumber = Math.random();
int d = (int) (randNumber * 100 );
int randomInt = (int)d * 2;
textBox.setText("" + randomInt);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With