与ViewTreeObserver onPreDrawLstener一起使用ConcurrentModificationException

时间:2014-08-31 23:32:16

标签: java android hashmap

我的方法

中创建了HashMap
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() {
        .
        .  
        .
    }
)};

并在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
    }

1 个答案:

答案 0 :(得分:2)

除非使用迭代器并使用其remove()方法,否则在迭代其keySet时,永远不能从hashMap中删除条目。这是ConcurrentModificationException的来源。它与线程无关。