I am trying to print a randomly generated integer to the screen. I looked at a couple other people’s questions on how to do it and here is my attempt:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_program);
TextView currentNumber = new TextView(this);
// randNum() generates the random number and returns it (returns int)
currentNumber.setText(randNum());
this.setContentView(currentNumber);
}
When I try to open the current activity, however, (emulator) the app stops working. What am I doing wrong? Also, I will be planning on allowing the user to generate a new random number later on and I want the display to update the currentNumber variable (overwriting the first number). Is this the best way of doing it or is there a better way?
randNum()I assume is anint. When you pass aninttosetText(), it interprets as a resource, which you don’t want.Try this:
Or, if you prefer (less efficient, but maybe more readable):
NB: Also, when you get crashes in the future, please post the full red section of the LogCat for us to help you debug.