Java – How do I output an integer using the TextView method?

How do I output an integer using the TextView method?… here is a solution to the problem.

How do I output an integer using the TextView method?

I want to output a random number in an Activity that is always even!
This is my code, what’s 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);

Solution

You need to initialize the TextView in the onCreate() method. Your code looks 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);

}

Related Problems and Solutions