while (stringBuffer.indexOf("AAC1") != -1 && stringBuffer.indexOf("EEED") != -1) {
LogUtil.logShipment(TAG, "AA 接受到的数据: " + stringBuffer.toString());
if (stringBuffer.indexOf("AAC1", 0) < (stringBuffer.indexOf("EEED", 0) + 4)){
String s = stringBuffer.substring(stringBuffer.indexOf("AAC1",0),stringBuffer.indexOf("EEED",0) + 4);
if (s != null && !s.isEmpty()) {
mHandler.post(new ScanEndRunnable(s));
stringBuffer.delete(stringBuffer.indexOf("AAC1", 0), stringBuffer.indexOf("EEED", 0) + 4);
}
} else {
stringBuffer.delete(0, stringBuffer.indexOf("AAC1", 0));
}
}
报错如下
java.lang.StringIndexOutOfBoundsException: length=0; regionStart=-1; regionLength=4
at java.lang.AbstractStringBuilder.startEndAndLength(AbstractStringBuilder.java:211)
at java.lang.AbstractStringBuilder.substring(AbstractStringBuilder.java:616)
at java.lang.StringBuffer.substring(StringBuffer.java:718)
String s = stringBuffer.substring(stringBuffer.indexOf("AAC1",0),stringBuffer.indexOf("EEED",0) + 4);为什么会报这种错呢?代码哪出问题了?