2014-08-31 31 views
0

我有得到我的方法創建的HashMapConcurrentModificationException的與ViewTreeObserver onPreDrawLstener

final HashMap<Long, Rect> listViewItemBounds = new HashMap<Long, Rect>(); 

,節省的可見視圖所有的界限在ListView

for (int i = 0; i < getChildCount(); ++i) { 
    View child = getChildAt(i); 
    if(child != null){ 
     int position = firstVisiblePosition+i; 
     long itemID = adapter.getItemId(position); 
     Rect startRect = new Rect(child.getLeft(), child.getTop(), child.getRight(),child.getBottom()); 
     listViewItemBounds.put(itemID, startRect); 
     listViewItemDrawables.put(itemID, getBitmapDrawableFromView(child)); 
    } 

} 

然後我在ListView控件啓動OnPreDrawListener

final ViewTreeObserver observer = getViewTreeObserver(); 
    observer.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() { 
    public boolean onPreDraw() { 
     . 
     . 
     . 
    } 
)}; 

and in the通過他onPreDrawListener我環路HashMap來獲取已保存的視圖邊界

for (Long itemId: listViewItemBounds.keySet()) { //points here 
    . 
    . 
    . 
    istViewItemBounds.remove(itemId); 
} 

當我改變/更新,其中這一切都被一次onPreDraw稱爲當前通過HashMap的循環,而它正在更新適配器問題就來了再次由於適配器的變化。

那麼我該如何防止這裏的併發,所以我沒有得到ConcurrentModificationException

堆棧跟蹤:

08-31 19:09:41.398 29127-29127/? E/AndroidRuntime﹕ FATAL EXCEPTION: main 
    java.util.ConcurrentModificationException 
      at java.util.HashMap$HashIterator.nextEntry(HashMap.java:792) 
      at java.util.HashMap$KeyIterator.next(HashMap.java:819) 
      at com.tyczj.myapp.views.HeaderGridView$3.onPreDraw(HeaderGridView.java:511) 
      at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:680) 
      at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1842) 
      at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989) 
      at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351) 
      at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749) 
      at android.view.Choreographer.doCallbacks(Choreographer.java:562) 
      at android.view.Choreographer.doFrame(Choreographer.java:532) 
      at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735) 
      at android.os.Handler.handleCallback(Handler.java:725) 
      at android.os.Handler.dispatchMessage(Handler.java:92) 
      at android.os.Looper.loop(Looper.java:137) 
      at android.app.ActivityThread.main(ActivityThread.java:5041) 
      at java.lang.reflect.Method.invokeNative(Native Method) 
      at java.lang.reflect.Method.invoke(Method.java:511) 
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
      at dalvik.system.NativeStart.main(Native Method) 

編輯:

Iterator<Long> it = listViewItemBounds.keySet().iterator(); 
    while(it.hasNext()){ 
     long itemId = it.next(); //error 
    } 

回答

2

在遍歷它的keySet除非你做它使用迭代器並使用它的remove()方法你永遠不能從一個HashMap中刪除條目。這是ConcurrentModificationException的來源。這與線程無關。

+0

你是對的,我沒有。 Mea culpa沒有看到remove()調用。 – 2014-09-01 00:04:59

+0

不用擔心。我第一次嘗試回答這個問題並不完全清楚 - 我試圖在移動設備上輸入代碼,但獲得的成功有限。 – 2014-09-01 00:12:27

+0

ok看到我的編輯,我改變了使用'Iterator',但現在我得到'it.next()'的錯誤,我錯過了什麼? – tyczj 2014-09-01 00:15:22

相關問題