在我的应用中,我想跟踪EditText
中的输入。每次用户修改我要跟踪的EditText
中的字符时:
我使用TextWatcher
来实现这一目标。到目前为止,我的方法一直很好。但是最近我在我的制作应用中遇到了IndexOutOfBoundsException
的3次崩溃。我注意到的一件事是,所有崩溃都发生在具有OS版本6.0.1的Galaxy S7上。
我正在试图找出理解和解决此崩溃的好方法。请参考以下代码
public class EditTextMonitor extends Activity implements TextWatcher {
private EditText etInputText;
private int deleteCharIndex = -1, addCharIndex = -1;
private char deleteChar;
private boolean wasCharDeleted;
private String prevCharSeq = "";
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mylayout);
etInputText = (EditText) findViewById(R.id.etInputText);
}
// Add TextChangedListener here to prevent call to text watcher methods upon switching
// orientation
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
etInputText.addTextChangedListener(this);
}
/*
This method is called to notify you that, within s, the count characters beginning at
start have just replaced old text that had length before. It is an error to attempt to
make changes to s from this callback.
*/
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
/*
This method is called to notify you that, within s, the count characters beginning at start
are about to be replaced by new text with length after.
It is an error to attempt to make changes to s from this callback.
*/
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// if count > after then its a char delete cause the no. of chars in 's'
// will decrease when the text change is complete.
// If count < after
// then its a char addition cause the no. of chars in 's'
// will increase when the text change is complete.
if (count > after) {
// CHARACTER DELETION
// As 'count' is no. of chars being replaced from 'start' by
// 'after' no. of chars, (start+count)-1 will be the index of the
// char that that will be deleted.
deleteCharIndex = (start + count) - 1;
deleteChar = s.charAt(deleteCharIndex);
wasCharDeleted = true;
} else if (count < after) {
// CHARACTER ADDITION
// As 'count' is no. of chars being replaced from 'start' by
// 'after' no. of chars, (start+after)-1 will will be the index of the
// char that will be added.
wasCharDeleted = false;
addCharIndex = (start + after) - 1;
} else {
// Extra call to the text changed method with no change in 's'.
// Android framework bug??
wasCharDeleted = false;
Log.d(TAG, "------EXTRA CALL TO BEFORETEXTCHANGED------");
}
}
@Override
public void afterTextChanged(Editable s) {
// Don't process if redundant call to afterTextChanged method.
// A framework bug??
if (!prevCharSeq.equals(s.toString())) {
if (wasCharDeleted) {
// handle char delete
if (deleteChar == 'x'
&& (s.length() == 0 || s.charAt(deleteCharIndex - 1) == ' ')) {
// Business logic to deal with the case where the deleted char was an independent 'x'
} else {
// Business logic to deal with the case where deleted char was anything other than an independent 'x'.
}
} else {
// handle char addition
***if (s.charAt(addCharIndex) == 'x'// CRASH OCCURS ON THIS LINE <-----------------***
&& (s.length() == 1 || s.charAt(addCharIndex - 1) == ' ')) {
// Business logic to deal with the case where added char is an independent 'x'
} else {
// Business logic to deal with the case where added char is anything other than an independent 'x'
}
}
}
prevCharSeq = s.toString();
}
}
迄今为止的3次崩溃是:
java.lang.IndexOutOfBoundsException: charAt: 24 >= length 23
java.lang.IndexOutOfBoundsException: charAt: 202 >= length 198
java.lang.IndexOutOfBoundsException: charAt: 1 >= length 1
查看异常中的索引,我假设addCharIndex
未正确计算。这意味着我对beforeTextChanged
的参数的理解不正确或者TextWatcher
方法没有以预期的顺序被调用,或者它们被更新的参数多次调用,这弄乱了我的逻辑来计算addCharIndex
中的beforeTextChanged
。
对于如何解决此问题的任何帮助和见解将不胜感激。
感谢。
答案 0 :(得分:1)
这不是设备问题。这是一个逻辑问题。
您的问题实际上发生在此之前的行上。因为当您删除字符时,您不会更新addCharIndex
。如果上一个索引是10并且您删除了8个字符,则索引仍为10,但您的长度仅为2.
如果您在输入中输入xd
,请删除x
indexOutOfBoundsException
因s.charAt(deleteCharIndex - 1) == ' '
而导致崩溃,因为您的if语句确实存在以下内容:
x
但是,由于您已删除了s.length() > 1
字符,因此您的长度现在为1,但您正在检查0或上述语句,这就是您收到错误的方式。如果你不知道if语句中是否有OR(||),它将检查每个条件,直到它达到真实结果。
有很多方法可以解决这个问题,一种方法是当你deleteCharIndex - 1
达到varFileXML = new System.Xml.XmlDocument();
varFileXML.LoadXml("<FileNode><FileName>FileName_0</FileName></FileNode>");
Msg_FileSchema = varFileXML;
时可以检查varFileName = Msg_FlatFileSchema(FILE.ReceivedFileName);
varFileName = System.IO.Path.GetFileName(varFileName);
最低点为0. {/ p>