[英]IndexOutOfBoundsException in LinkedList
我收到了 IndexOutOfBoundsException,這對我來說似乎是不可能的。
代碼:
public class SomethingCalculator {
@Nullable
private Config mConfig;
@Nullable
private Long mTime;
final private List<Long> mLinkedList = new LinkedList<>();
public synchronized void setupWithConfiguration(Config config, Long time) {
//config and time are non null always
mTime = time;
mConfig = config;
generatLookUp();
}
public synchronized void reset() {
mConfig = null;
mTime = null;
mLinkedList.clear();
}
@Nullable
public synchronized Long getTheValue(long ms) {
if (mConfig == null)
return null;
// getting exception here
if (ms > mLinkedList.get(mLinkedList.size() - 1)) {
return 0l; // something
}
return 0l; // something
}
private synchronized void generatLookUp() {
mLinkedList.clear();
if (mConfig == null || mTime==null)
return;
// mLinkedList will always have size > 0 after executing this
// based upon config this method add elements to mLinkedList
// adding dummy values
mLinkedList.add(1L);
mLinkedList.add(2L);
}
}
調用SomethingCalculator.getTheValue()
出現異常
例外:
Fatal Exception: java.lang.IndexOutOfBoundsException: Index: 4, Size: 0
at java.util.LinkedList.checkElementIndex(LinkedList.java:565)
不確定 mMylinkedList.size() 是否返回 4 那么 mMylinkedList.get(4) 如何拋出 IndexOutOfBoundsException。
查看生成的 AAR 后發現, synchronized
關鍵字被剝離,因此可能會發生此異常。 由於 proguard 優化, synchronized
關鍵字被剝離。
刪除了這個:
-optimizations !method/marking/synchronized
現在我可以在生成的 AAR 中看到synchronized
關鍵字。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.