-1

I have gone through many articles and found similar, but have not found any that have allowed me to fix the issue.

I am trying to create a random number generator and I cannot get the random number to get into the text field. This seems so simple and I'm not sure what is going on. The "setText" is constantly throwing an error "Cannot resolve method 'setText(Java.lang.String)"

The code within my random might be a little odd. I have been trying different things to make it work and this is the most recent attempt.

private Randomizer mRandomizer = new Randomizer();

Button rollButton;
GridLayout gridLayout;
Spinner fourspinner;
Spinner sixspinner;
Spinner eightspinner;
Spinner tenspinner;
Spinner twentyspinner;
Spinner hundredspinner;
TextView resultText;
Random randDice;


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

    rollButton = (Button)findViewById(R.id.rollButton);
    gridLayout = (GridLayout)findViewById(R.id.gridLayout);
    fourspinner = (Spinner)findViewById(R.id.fourspinner);
    sixspinner = (Spinner)findViewById(R.id.sixspinner);
    eightspinner = (Spinner)findViewById(R.id.eightspinner);
    tenspinner = (Spinner)findViewById(R.id.tenspinner);
    twentyspinner = (Spinner)findViewById(R.id.twentyspinner);
    hundredspinner = (Spinner)findViewById(R.id.hundredspinner);
    resultText = (TextView)findViewById(R.id.resultText);

    View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // click the Button!

            String resultText = "";

                randDice = new Random();
            for(int i = 0; i < 10; i++){
                resultText += String.valueOf(randDice.nextInt()) + "\n";
            }

                resultText.setText(""+resultText);
                //resultText.(diceRoll);

        }
    };
    rollButton.setOnClickListener(listener);
}
Andrew
  • 3
  • 1

3 Answers3

0

This is because you are referencing local variable resultText String to set it as textview. It cannot reference to textview you have declared in class because inside listener it refers to string.

Change the textView i.e

 resultText = (TextView)findViewById(R.id.resultText);

to something different like

 resultTextTextView = (TextView)findViewById(R.id.resultText);

and use it as

resultTextTextView.setText(""+resultText);

or use the class of activity or fragment prefix like

MyActivity.this.resultText.setText(""+resultText);
subhash
  • 2,689
  • 18
  • 13
0

You should rename your TextView or your String "resultText" - it won't work if you use the same variable name twice. The problem is, that you try to call setText on your String and not on your TextView, since you are declaring "resultText" to be a String in your onCreate method.

yennsarah
  • 5,467
  • 2
  • 27
  • 48
0

You have declared two variables with the same name:

TextView resultText

and

String resultText

In practice, the String variable (which is declared inside the method) is hiding the TextView variable. To fix the problem, rename the local string variable (and its usages) into e.g.

String result
Mick Mnemonic
  • 7,808
  • 2
  • 26
  • 30