0

I am trying to make a basic dice roller for a card game. It is on the second screen of two. The screen opens fine when I press the button to open it, but when I click the dice button the app simply crashes. I am using the newest version of Android Studio. Here's my code:

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;
import java.util.Random;
import static android.R.attr.value;

public class Extras extends AppCompatActivity implements OnClickListener{

    Button btn1;
    TextView numberGenerator;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_extras);


        btn1 = (Button) findViewById(R.id.diceButton);

        btn1.setOnClickListener(this);
        numberGenerator = (TextView)findViewById(R.id.numberGenerator);

    }


    @Override

    public void onClick(View v) {
        int min = 1;
        int max = 6;
        Random random = new Random();
        int value = random.nextInt(max - min) + min;
        numberGenerator.setText(value+"");

        if (v == btn1) {
            numberGenerator.setText(value);
        }

    }
    ...
}
Daniel
  • 2,355
  • 9
  • 23
  • 30
Stoot
  • 1
  • 1
  • 1
    Usually when an application "crashes" it shows an error message or exception message. If yours does this, you will want to share this with us. – Hovercraft Full Of Eels Apr 01 '17 at 16:35
  • 1
    a useful read for a new android dev http://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this – trooper Apr 01 '17 at 16:50

1 Answers1

2

Your app is crashes because you are trying to put int value to the setText() method.

Here:

if (v == btn1) {
    numberGenerator.setText(value);
}

Try instead:

if (v == btn1) {
    numberGenerator.setText(String.valueOf(value));
}

If this does not work post your stacktrace.

Mark
  • 9,604
  • 5
  • 36
  • 64
Alex Kamenkov
  • 891
  • 1
  • 6
  • 16
  • I would think this is a compiler kind of complaint – bichito Apr 01 '17 at 16:45
  • this answers it. `setText` is overloaded to take an `int` resource identifier, so you can't just pass any old `int` into it. https://developer.android.com/reference/android/widget/TextView.html#setText(int) – trooper Apr 01 '17 at 16:48