dimanche 5 novembre 2017

Crash when trying to display a random number

So I have an android app doing a few random thing to just teach myself how to do things. I am trying to go from one activity to another, and when the second activity is created it generates and displays a random number between one and ten. This is the button changing activities.

Button number = (Button) findViewById(R.id.pickNum);
    number.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startActivity(new Intent(MainActivity.this, Third.class));
        }
    });}

Then in the onCreate method in the new activity I have this.

 tv = (TextView) findViewById(R.id.genNumber);
    setContentView(R.layout.activity_third);
    Random rand = new Random();
    int randomNum = rand.nextInt((max - min) + 1) + min;
    //tv.setText(String.format("%d",randomNum));
    //tv.setText(""+randomNum);
    //tv.setText(String.valueOf(randomNum));

The commented out code is the ways I have tried and none of them work. When I leave anyone of them in it just crashes, can anyone explain?




Aucun commentaire:

Enregistrer un commentaire