Concurrent Modification Exception : adding to an ArrayList

前端 未结 10 904
醉梦人生
醉梦人生 2020-11-22 10:53

The problem occurs at

Element element = it.next();

And this code which contains that line, is inside of an OnTouchEvent

10条回答
  •  名媛妹妹
    2020-11-22 11:23

    I solved creating a lock (Kotlin):

    import java.util.concurrent.locks.ReentrantLock
    
    Class A {
        private val listLock = ReentrantLock()
        fun doSomething(newElement){
            listLock.lock()
            list.add(newElement)
            listLock.unlock()
        }
    }
    

提交回复
热议问题