-2

Fairly new so sorry for the stupid question.. I'm trying to update the TextView as soon as the EditText loses focus. The Edittext needs to convert to an int to calculate what the textview should show. As soon as the EditText loses focus the app crashes.

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

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


    final EditText strScore = (EditText) findViewById(R.id.strScore);
    final TextView strMod = findViewById(R.id.strMod);



        strScore.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View strScoreView, boolean hasFocus) {
                if (!hasFocus){
                    updateModifier(strMod,strScore);
                }
            }
        });
    }
    private void updateModifier(TextView modifier, EditText score){
        int scoreInt = Integer.parseInt(score.getText().toString());
        int modifierInt = scoreInt - 10 / 2;
        modifier.setText(modifierInt);
    }
}
Zoe
  • 27,060
  • 21
  • 118
  • 148
  • 2
    _needs to convert to an int_ Probably `NumberFormatException`. Are you getting an exception? If yes, post the details including the stack trace which you can get from the log as explained in this _Stack Overflow_ question: [Is it a bad idea to use printStackTrace() for caugt Exceptions?](https://stackoverflow.com/questions/3855187/is-it-a-bad-idea-to-use-printstacktrace-for-caugt-exceptions) – Abra Jun 21 '19 at 05:44
  • Share the error – Shivam Oberoi Jun 21 '19 at 07:08
  • @HelloWorld Do the change mentioned in GuruCharan answer it will help you fix ur program – Jeeva Jun 21 '19 at 08:14
  • @HelloWorld please post stack trace from logcat next time as mentioned by others in comment – Jeeva Jun 21 '19 at 08:15

1 Answers1

0

try using

modifier.setText(String.valueOf(modifierInt));

in updateModifier() method, because you are trying to set Integer value to Textview which accepts only Strings

GuruCharan
  • 139
  • 1
  • 14
  • the reason the op is getting exception because he is trying to set integer value to textview which is mentioned by this answerer.if the op just makes the change his program will get executed successfully – Jeeva Jun 21 '19 at 08:13
  • that was exactly right, thank you – HelloWorld Jun 21 '19 at 16:58