0

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

Community
  • 1
  • 1
Jackherer
  • 47
  • 1
  • 8
  • Those methods take parameters... Read the documentation. The method can be resolved. The arguments to it (none) cannot be applied. – OneCricketeer Oct 20 '16 at 02:59
  • @cricket_007 Hey thanks for your quick comment, so it wants me to put a parameter to the setText() method such as setText("@string/hello_world") as an example? I do not understand "The arguments to it cannot be applied.". Sorry as I said I am a noob to Java, as in I just started learning yesterday! – Jackherer Oct 20 '16 at 03:09
  • Well, `setText(R.string.hello_world)` would get you that value (if that is what you are after). What you wrote would be written literally to the EditText. But yes, you'll need to provide a string literal or string resource – OneCricketeer Oct 20 '16 at 03:13
  • The linked question does have parameters in those methods. Why did you remove them? – OneCricketeer Oct 20 '16 at 03:16
  • @cricket_007 look at my answer... – Jackherer Oct 20 '16 at 03:19

3 Answers3

1

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)

Rajesh Gopu
  • 863
  • 9
  • 33
0

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

Kyal Bond
  • 296
  • 1
  • 12
0

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);
    }
Jackherer
  • 47
  • 1
  • 8