0

I am trying to get my first app running on Android studio. The app instantiates and runs the setOnClickListener.

When I try to enter input for the addTextChangeListener it crashes.

"I have stripped back the code, to find where the fault is, but I still can't figure it out. I am new at this, being my first app.

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

        EditText ageText = (EditText) findViewById(R.id.userAge);
        ageText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {


            }

            @Override
            public void afterTextChanged(Editable s) {

                int age = Integer.parseInt(userAge.getText().toString());
                int intYears = Integer.valueOf(userYears.toString());
                String text;

                if (age < intYears/3){
                    text = getString(R.string.Message_1, userAge);
                    outputText.setText(text);
                }
                else{
                    text = getString(R.string.Message_2, userAge);
                    outputText.setText(text);
                }
            }
        });

Expected: Enter an int m, convert int m to another int m' display m'. Enter int n in another text field, compare int n and m and display a text.

Result: Executes first part, app crashes when I try and enter int n in the other text field.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
emag_mI
  • 5
  • 2

1 Answers1

1

You missed getText().

int intYears = Integer.valueOf(userYears.getText().toString());

pRaNaY
  • 24,642
  • 24
  • 96
  • 146
Ashok Kumar
  • 1,226
  • 1
  • 10
  • 14
  • Also, this part: text = getString(R.string.Message_1, userAge); it should be age instead of userAge, userAge is your textView, and you want to put the age to the string – Sander Rito Sep 24 '19 at 06:08
  • I forgot getText(). I tried ```text = getString(R.string.Message_1, age)``` first, cos it seems natural to me as well. The IDE throws error ```suspicious argument type for formatting argument #1 in Message_1:conversion is s, received int(argument #2 in method call)``` – emag_mI Sep 24 '19 at 16:41