Reputation: 47
I am a complete noob to java programming and have thrown myself in at the deep end, as per usual (I seem to get better results that way).
I am creating an Android app with Android Studio and have come across an error in my Java code.
I have searched for an answer but the answers are all very specific to the actual code so cannot find any answers on my own.
My problem arises in my MainActivity.java where i am using TextWatcher as a NumberTextWatcher that will convert and set any input to decimal currency (as the user will be inputting a price) from this answer on Stack Overflow.
The problem is that I am getting an error:
Cannot resolve method setText()
Here is the code where the error is (I have marked the errors with //<- Here it throws an error.):
public class NumberTextWatcher implements TextWatcher {
private final DecimalFormat df;
private final DecimalFormat dfnd;
private final EditText et;
private boolean hasFractionalPart;
private int trailingZeroCount;
NumberTextWatcher(EditText inputPricePerOz, String pattern) {
df = new DecimalFormat(pattern);
df.setDecimalSeparatorAlwaysShown(true);
dfnd = new DecimalFormat("#,###.00");
this.et = inputPricePerOz;
hasFractionalPart = false;
}
@Override
public void afterTextChanged(Editable s) {
et.removeTextChangedListener();
if (s != null && !s.toString().isEmpty()) {
try {
int inilen, endlen;
inilen = et.getText().length();
String v = s.toString().replace(String.valueOf(df.getDecimalFormatSymbols().getGroupingSeparator()), "").replace("$","");
Number n = df.parse(v);
int cp = et.getSelectionStart();
if (hasFractionalPart) {
StringBuilder trailingZeros = new StringBuilder();
while (trailingZeroCount-- > 0)
trailingZeros.append('0');
et.setText(); //<- Here it throws an error.
} else {
et.setText(); //<- Here it throws an error.
}
et.setText(); //<- Here it throws an error.
endlen = et.getText().length();
int sel = (cp + (endlen - inilen));
if (sel > 0 && sel < et.getText().length()) {
et.setSelection(sel);
} else if (trailingZeroCount > -1) {
et.setSelection(et.getText().length() - 3);
} else {
et.setSelection(et.getText().length());
}
} catch (NumberFormatException | ParseException e) {
e.printStackTrace();
}
}
et.addTextChangedListener(this);
}
As I know very little about Java I do not know why this error is occurring, although I would think that setting text with the setText() method would be simple enough. Any help would be very much appreciated
Upvotes: 0
Views: 2664
Reputation: 47
So I figured out why the error is being thrown out, its a simple mistake that I cannot explain but when I copied and pasted the code it somehow missed out copying certain parts, or failing that android studio/gradle changed my code, here is the fixed code:
@Override
public void afterTextChanged(Editable s) {
et.removeTextChangedListener(this); //<- also fixed error an error here
if (s != null && !s.toString().isEmpty()) {
try {
int inilen, endlen;
inilen = et.getText().length();
String v = s.toString().replace(String.valueOf(df.getDecimalFormatSymbols().getGroupingSeparator()), "").replace("$","");
Number n = df.parse(v);
int cp = et.getSelectionStart();
if (hasFractionalPart) {
StringBuilder trailingZeros = new StringBuilder();
while (trailingZeroCount-- > 0)
trailingZeros.append('0');
et.setText(df.format(n) + trailingZeros.toString()); //<- Fixed error
} else {
et.setText(dfnd.format(n)); //<- Fixed error
}
et.setText("$".concat(et.getText().toString())); //<- Fixed error
endlen = et.getText().length();
int sel = (cp + (endlen - inilen));
if (sel > 0 && sel < et.getText().length()) {
et.setSelection(sel);
} else if (trailingZeroCount > -1) {
et.setSelection(et.getText().length() - 3);
} else {
et.setSelection(et.getText().length());
}
} catch (NumberFormatException | ParseException e) {
e.printStackTrace();
}
}
et.addTextChangedListener(this);
}
Upvotes: 0
Reputation: 893
Wrong syntax: It should be like below any one
et.setText("Text to set in editext");
OR
et.setText(R.string.app_name);
OR
et.setText("Text to set in editext", BufferType.EDITABLE);
Refer the API Doc :
https://developer.android.com/reference/android/widget/EditText.html#setText(java.lang.CharSequence, android.widget.TextView.BufferType)
&
https://developer.android.com/reference/android/widget/TextView.html#setText(java.lang.CharSequence)
Upvotes: 1
Reputation: 296
If you check the documentation for EditText you will see that the method takes a string and a buffer type. So you need to add the text you wish to change to and the buffertype. You probably want TextView.BufferType.EDITABLE.
e.g.
et.setText("Example text", TextView.BufferType.EDITABLE);
see more here https://developer.android.com/reference/android/widget/EditText.html
Upvotes: 0