Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to output a integer with TextView methods?

Tags:

java

android

xml

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);
like image 369
Agon Avdijaj Avatar asked Nov 10 '22 11:11

Agon Avdijaj


1 Answers

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

}
like image 63
Bidhan Avatar answered Nov 14 '22 21:11

Bidhan