java - StackOverflow error in TextWatcher watcher in EditText -


i'm converting amount text edittext double format , im trying remove "," in edittext execute proper format computations. what's wrong code , having stack overflow?

// gets 2 edittext controls' editable values  string cleanbasictax; if(txtbasictax.gettext().tostring().contains(",")){     cleanbasictax=txtbasictax.gettext().tostring().replace(",", ""); } else{     cleanbasictax=txtbasictax.gettext().tostring(); }  string cleantxtsurcharge; if(txtsurcharge.gettext().tostring().contains(",")){     cleantxtsurcharge=txtsurcharge.gettext().tostring().replace(",", ""); } else{     cleantxtsurcharge=txtsurcharge.gettext().tostring(); }  string cleaninterest; if(txtinterest.gettext().tostring().contains(",")){     cleaninterest=txtinterest.gettext().tostring().replace(",", ""); } else{     cleaninterest=txtinterest.gettext().tostring(); }  string cleancompromise=txtcompromise.gettext().tostring().replace(",", ""); if(txtcompromise.gettext().tostring().contains(",")){     cleancompromise=txtcompromise.gettext().tostring().replace(",", ""); } else{     cleancompromise=txtcompromise.gettext().tostring(); }             editable editablevaluebasictax = txtbasictax.gettext().replace(0, txtbasictax.length(), cleanbasictax),     editablevaluesurcharge = txtsurcharge.gettext().replace(0, txtsurcharge.length(), cleantxtsurcharge),     editablevalueinterest = txtinterest.gettext().replace(0, txtinterest.length(), cleaninterest),     editablevaluecompromise = txtcompromise.gettext().replace(0, txtcompromise.length(), cleancompromise); 

your textwathcer changes text watching, causing call watcher, causing change text, ... , on until run out of stack space invoking methods.

one way fix set boolean flag true when you're running inside watcher , return if flag set kill recursion. in pseudocode:

boolean misinwatcher = false;  void ontextchanged(...) {    if (misinwatcher) return;    misinwatcher = true;     // text modifications here     misinwatcher = false; } 

Comments

Popular posts from this blog

python - Subclassed QStyledItemDelegate ignores Stylesheet -

java - HttpClient 3.1 Connection pooling vs HttpClient 4.3.2 -

SQL: Divide the sum of values in one table with the count of rows in another -