gpt4 book ai didi

java - ViewTreeObserver onPreDrawListener 发生 ConcurrentModificationException

转载 作者:行者123 更新时间:2023-12-02 05:30:28 25 4
gpt4 key购买 nike

我有一个在我的方法中创建的 HashMap

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

将可见 View 的所有边界保存在 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 以获取已保存 View 的边界

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
}

最佳答案

在迭代其 keySet 时,您永远无法从 hashMap 中删除条目,除非您使用迭代器并使用其remove() 方法来执行此操作。这就是 ConcurrentModificationException 的来源。与线程无关。

关于java - ViewTreeObserver onPreDrawListener 发生 ConcurrentModificationException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25597211/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com