0

I'm trying to do it but I get a "Stack Overflow error" so I accepted the advice and came here searching for help.

I want to limit the lenght of the text the user can insert, so when he type a character above the length limit the character is erased

sms_input.addTextChangedListener(new TextWatcher(){

        @Override
        public void afterTextChanged(Editable s) {

            String cs = sms_input.getText().toString();
            if (cs.length() > 160) { sms_input.setText(cs.substring(0, MAX_MSG_LENGTH + 1)); }

        }

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

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before,
                int count) {
            // TODO Auto-generated method stub

        }

    });

Logcat:

09-25 17:07:34.972: E/AndroidRuntime(22733): FATAL EXCEPTION: main

09-25 17:07:34.972: E/AndroidRuntime(22733): java.lang.StackOverflowError 09-25 17:07:34.972: E/AndroidRuntime(22733): at android.graphics.Paint.getTextRunAdvances(Paint.java:1754) 09-25 17:07:34.972: E/AndroidRuntime(22733): at android.graphics.Paint.getTextRunAdvances(Paint.java:1720) 09-25 17:07:34.972: E/AndroidRuntime(22733): at android.text.MeasuredText.addStyleRun(MeasuredText.java:164) 09-25 17:07:34.972: E/AndroidRuntime(22733): at android.text.MeasuredText.addStyleRun(MeasuredText.java:204) 09-25 17:07:34.972: E/AndroidRuntime(22733): at android.text.StaticLayout.generate(StaticLayout.java:281) 09-25 17:07:34.972: E/AndroidRuntime(22733): at android.text.DynamicLayout.reflow(DynamicLayout.java:284) 09-25 17:07:34.972: E/AndroidRuntime(22733): at android.text.DynamicLayout.(DynamicLayout.java:170) 09-25 17:07:34.972: E/AndroidRuntime(22733): at android.widget.TextView.makeSingleLayout(TextView.java:5986) 09-25 17:07:34.972: E/AndroidRuntime(22733): at android.widget.TextView.makeNewLayout(TextView.java:5884) 09-25 17:07:34.972: E/AndroidRuntime(22733): at android.widget.TextView.checkForRelayout(TextView.java:6423) 09-25 17:07:34.972: E/AndroidRuntime(22733): at android.widget.TextView.setText(TextView.java:3696) 09-25 17:07:34.972: E/AndroidRuntime(22733): at android.widget.TextView.setText(TextView.java:3554) 09-25 17:07:34.972: E/AndroidRuntime(22733): at android.widget.EditText.setText(EditText.java:80) 09-25 17:07:34.972: E/AndroidRuntime(22733): at android.widget.TextView.setText(TextView.java:3529) 09-25 17:07:34.972: E/AndroidRuntime(22733): at com.easyray.lemon.SmsChat$5.afterTextChanged(SmsChat.java:211) 09-25 17:07:34.972: E/AndroidRuntime(22733): at android.widget.TextView.sendAfterTextChanged(TextView.java:7247) 09-25 17:07:34.972: E/AndroidRuntime(22733): at android.widget.TextView.setText(TextView.java:3703) 09-25 17:07:34.972: E/AndroidRuntime(22733): at android.widget.TextView.setText(TextView.java:3554) 09-25 17:07:34.972: E/AndroidRuntime(22733): at android.widget.EditText.setText(EditText.java:80) 09-25 17:07:34.972: E/AndroidRuntime(22733): at android.widget.TextView.setText(TextView.java:3529) 09-25 17:07:34.972: E/AndroidRuntime(22733): at com.easyray.lemon.SmsChat$5.afterTextChanged(SmsChat.java:211) 09-25 17:07:34.972: E/AndroidRuntime(22733): at android.widget.TextView.sendAfterTextChanged(TextView.java:7247) 09-25 17:07:34.972: E/AndroidRuntime(22733): at android.widget.TextView.setText(TextView.java:3703) 09-25 17:07:34.972: E/AndroidRuntime(22733): at android.widget.TextView.setText(TextView.java:3554) 09-25 17:07:34.972: E/AndroidRuntime(22733): at android.widget.EditText.setText(EditText.java:80) 09-25 17:07:34.972: E/AndroidRuntime(22733): at android.widget.TextView.setText(TextView.java:3529) 09-25 17:07:34.972: E/AndroidRuntime(22733): at com.easyray.lemon.SmsChat$5.afterTextChanged(SmsChat.java:211) 09-25 17:07:34.972: E/AndroidRuntime(22733): at android.widget.TextView.sendAfterTextChanged(TextView.java:7247) 09-25 17:07:34.972: E/AndroidRuntime(22733): at android.widget.TextView.setText(TextView.java:3703) 09-25 17:07:34.972: E/AndroidRuntime(22733): at android.widget.TextView.setText(TextView.java:3554) 09-25 17:07:34.972: E/AndroidRuntime(22733): at android.widget.EditText.setText(EditText.java:80) 09-25 17:07:34.972: E/AndroidRuntime(22733): at android.widget.TextView.setText(TextView.java:3529) 09-25 17:07:34.972: E/AndroidRuntime(22733): at com.easyray.lemon.SmsChat$5.afterTextChanged(SmsChat.java:211) 09-25 17:07:34.972: E/AndroidRuntime(22733): at android.widget.TextView.sendAfterTextChanged(TextView.java:7247) 09-25 17:07:34.972: E/AndroidRuntime(22733): at android.widget.TextView.setText(TextView.java:3703) 09-25 17:07:34.972: E/AndroidRuntime(22733): at android.widget.TextView.setText(TextView.java:3554) 09-25 17:07:34.972: E/AndroidRuntime(22733): at android.widget.EditText.setText(EditText.java:80) 09-25 17:07:34.972: E/AndroidRuntime(22733): at android.widget.TextView.setText(TextView.java:3529) 09-25 17:07:34.972: E/AndroidRuntime(22733): at com.easyray.lemon.SmsChat$5.afterTextChanged(SmsChat.java:211) 09-25 17:07:34.972: E/AndroidRuntime(22733): at android.widget.TextView.sendAfterTextChanged(TextView.java:7247) 09-25 17:07:34.972: E/AndroidRuntime(22733): at android.widget.TextView.setText(TextView.java:3703) 09-25 17:07:34.972: E/AndroidRuntime(22733): at android.widget.TextView.setText(TextView.java:3554) 09-25 17:07:34.972: E/AndroidRuntime(22733): at android.widget.EditText.setText(EditText.java:80) 09-25 17:07:34.972: E/AndroidRuntime(22733): at android.widget.TextView.setText(TextView.java:3529) 09-25 17:07:34.972: E/AndroidRuntime(22733): at com.easyray.lemon.SmsChat$5.afterTextChanged(SmsChat.java:211) 09-25 17:07:34.972: E/AndroidRuntime(22733): at android.widget.TextView.sendAfterTextChanged(TextView.java:7247) 09-25 17:07:34.972: E/AndroidRuntime(22733): at android.widget.TextView.setText(TextView.java:3703) 09-25 17:07:34.972: E/AndroidRuntime(22733): at android.widget.TextView.setText(TextView.java:3554) 09-25 17:07:34.972: E/AndroidRuntime(22733): at android.widget.EditText.setText(EditText.java:80) 09-25 17:07:34.972: E/AndroidRuntime(22733): at android.widget.TextView.setText(TextView.java:3529) 09-25 17:07:34.972: E/AndroidRuntime(22733): at com.easyray.lemon.SmsChat$5.afterTextChanged(SmsChat.java:211) 09-25 17:07:34.972: E/AndroidRuntime(22733): at android.widget.TextView.sendAfterTextChanged(TextView.java:7247) 09-25 17:07:34.972: E/AndroidRuntime(22733): at android.widget.TextView.setText(TextView.java:3703) 09-25 17:07:34.972: E/AndroidRuntime(22733): at android.widget.TextView.setText(TextView.java:3554) 09-25 17:07:34.972: E/AndroidRuntime(22733): at android.widget.EditText.setText(EditText.java:80) 09-25 17:07:34.972: E/AndroidRuntime(22733): at android.widget.TextView.setText(TextView.java:3529) 09-25 17:07:34.972: E/AndroidRuntime(22733): at com.easyray.lemon.SmsChat$5.afterTextChanged(SmsChat.java:211) 09-25 17:07:34.972: E/AndroidRuntime(22733): at android.widget.TextView.sendAfterTextChanged(TextView.java:7247) 09-25 17:07:34.972: E/AndroidRuntime(22733): at android.widget.TextView.setText(TextView.java:3703) 09-25 17:07:34.972: E/AndroidRuntime(22733): at android.widget.TextView.setText(TextView.java:3554) 09-25 17:07:34.972: E/AndroidRuntime(22733): at android.widget.EditText.setText(EditText.java:80) 09-25 17:07:34.972: E/AndroidRuntime(22733): at android.widget.TextView.setText(TextView.java:3529) 09-25 17:07:34.972: E/AndroidRuntime(22733): at com.easyray.lemon.SmsChat$5.afterTextChanged(SmsChat.java:211) 09-25 17:07:34.972: E/AndroidRuntime(22733): at android.widget.TextView.sendAfterTextChanged(TextView.java:7247) 09-25 17:07:34.972: E/AndroidRuntime(22733): at android.widget.TextView.setText(TextView.java:3703) 09-25 17:07:34.972: E/AndroidRuntime(22733): at android.widget.TextView.setText(TextView.java:3554) 09-25 17:07:34.972: E/AndroidRuntime(22733): at android.w

4

2 回答 2

4

你不想做所有这些。只需在 XML 中试试这个

android:maxLength="120"

当您尝试输入超过 120 个字符时,它只会阻止您输入下一个字符。

我想这就是你要找的。如果没有就离开它。

于 2013-09-25T15:33:46.180 回答
3

之前已经回答了同样的问题,例如此处(TextWatcher afterTextChanged 导致 android 中的 stackoverflow

但基本上发生的情况是您更改方法“afterTextChanged”中的文本并触发再次调用该方法,从而引发以 StackOverflow 异常结束的无限循环:)

于 2013-09-25T15:14:10.720 回答