Stackoerverflowerror occurs in Android edittext settext

Source: Internet
Author: User

I encountered this problem in the previous project and solved it later. I encountered this problem again in this project. As a result, I had a problem for a day and asked my colleagues to open my door.

So write it down and remember it.

Android has an interface for listening input: textwatcher, which can be implemented by itself and then registered with edittext.

Class watcher implements textwatcher {

@ Override
Public void beforetextchanged (charsequence S, int start, int count,
Int after ){
// Todo auto-generated method stub
If (ischanged ){
Return;
}
}

@ Override
Public void ontextchanged (charsequence S, int start, int before,
Int count ){
// Todo auto-generated method stub

}

@ Override
Public void aftertextchanged (editable s ){
// Todo auto-generated method stub

Meditview. settext (SS );

}

}

According to the above, a bug will occur: stackoverflowerror.

The reason is that settext () itself calls back the listener. The after function in the listener calls settext, and the result is a mutual call without exit. The result is that the call function stack overflows.

Solution: Do not write the function that triggers the listener in the listener, especially the function that does not have an exit to call each other.

Second, you can write a flag. If the edittext content changes, let him execute the settext function. If the content does not change, the settext function in the listener will not be executed.

: Class watcher implements textwatcher {

Private Boolean ischanged = false;
@ Override
Public void beforetextchanged (charsequence S, int start, int count,
Int after ){
// Todo auto-generated method stub
If (ischanged ){
Return;
}
}

@ Override
Public void ontextchanged (charsequence S, int start, int before,
Int count ){
// Todo auto-generated method stub

}

@ Override
Public void aftertextchanged (editable s ){
// Todo auto-generated method stub
If (ischanged ){
Return;
}
String STR = S. tostring ();
Spannablestring Ss = chatemotion. string2symbol (smileactivity. This, STR );
Log. D (TAG, ss. tostring ());
Ischanged = true;
Meditview. settext (SS );
Ischanged = false;
Meditview. invalidate ();
}

}

This is OK. Haha, the flag position is true before set, and then the flag position is false.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.