Search code examples
androidmathmethodsstack-overflow

Substraction between Float


Please guys help me! I'm going crazy ! Below is a brief summary of my code that should be used to make a simple subtraction . Should I just read the amount of SCONTRINO and if you put CONTANTI , the field VINCITE , will have as setText SCONTRINO - CONTANTI , same with VINCITE , will CONTANTI.setText SCONTRINO - VINCITE . But despite everything seems to be well written , when I insert a field , I StackOverflowError by the two Update methods.

public class AssegnaScontoActivity extends Activity {
    TextView contanti;
    TextView vincite;
    TextView scontrino;
    Float contantiFloat;
    Float vinciteFloat;
    Float scontrinoFloat;


  public void onCreate(Bundle savedInstanceState) {
    //INIZIALIZZAZIONE ACTIVITTY
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.assegna_sconto_activity_landscape);
    //--------------------------  

    contantiFloat = Float.parseFloat(contanti.getText().toString());
    vinciteFloat = Float.parseFloat(vincite.getText().toString());
    scontrinoFloat = Float.parseFloat(1000);
    contanti = (TextView) findViewById(R.id.contanti);
    vincite = (TextView) findViewById(R.id.importo_vincite);
    scontrino = (TextView) findViewById(R.id.importo_scontrino);        


  contanti.addTextChangedListener(new TextChangedListener()
    {
        @Override
        public void numberEntered(Float number)
        {
            contantiFloat = number;
            updateVincite();

        }
    });
    vincite.addTextChangedListener(new TextChangedListener()
    {

        @Override
        public void numberEntered(Float number)
        {
            vinciteFloat = number;
            updateContanti();
        }
    });
    }

private void updateVincite()
{
    Float total = scontrinoFloat - contantiFloat; // This is where you apply your function
    vincite.setText(""+total); // need to do that otherwise int will
    // be treated as res id.
}

private void updateContanti()
{

    Float total = scontrinoFloat - vinciteFloat; // This is where you apply your function
    contanti.setText(""+total); // need to do that otherwise int will
    // be treated as res id.
}

private abstract class TextChangedListener implements TextWatcher
{

    public abstract void numberEntered(Float number);

    @Override
    public void afterTextChanged(Editable s)
    {
        String text = s.toString();
        try
        {
            Float parsedFloat = Float.parseFloat(text);
            numberEntered(parsedFloat);
        } catch (NumberFormatException e)
        {
            Log.w(getPackageName(), "Non si puo' parsare '" + text + "' col numero", e);
        }
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after)
    {
    }

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

Solution

  • Your code is going into an infinite loop because you are changing the text when afterTextChanged() is called, which causes afterTextChanged() to be called again and so on until eventually you overflow your call stack.

    You can stop this by only setting the text inside updateVincite() and updateContanti() if it is different to the current text.

    e.g.:

    private void updateVincite()
    {
        Float total = scontrinoFloat - contantiFloat; // This is where you apply your function
        String text = ""+total;
        if(!vincite.getText().toString().contentEquals(text))
            vincite.setText(text); // need to do that otherwise int will
        // be treated as res id.
    }
    

    and do the same for updateContanti()