for循环的ConcurrentModificationException

时间:2017-04-06 19:36:10

标签: android autocompletetextview custom-adapter

我将自定义适配器与AutoCompleteTextView视图结合使用,以在用户输入时填充下拉建议。但是它崩溃了,logcat在下面代码的for(Object object:result)行给我ConcurrentModificationException

@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
   parts.clear();
   if (results != null && results.count > 0) {
      List<?> result = (List<?>) results.values;
      for (Object object : result) {
          if (object instanceof Part) {
              parts.add((Part) object);
          }
      }
   } else if (constraint == null) {
       parts.addAll(partsAll);
   }
   notifyDataSetChanged();
}

根据我的理解,ConcurrentModificationException主要发生在尝试从正在迭代的列表中删除项目时。

这是logcat

  

04-06 14:35:01.483 25167-25167 / ryko.fstwo E / AndroidRuntime:FATAL   例外:主要                                                               过程:ryko.fstwo,PID:25167                                                               java.util.ConcurrentModificationException                                                                   at java.util.ArrayList $ ArrayListIterator.next(ArrayList.java:573)                                                                   在ryko.fstwo.adapters.PartAutoCompleteAdapter $ 1.publishResults(PartAutoCompleteAdapter.java:144)                                                                   在android.widget.Filter $ ResultsHandler.handleMessage(Filter.java:282)                                                                   在android.os.Handler.dispatchMessage(Handler.java:102)                                                                   在android.os.Looper.loop(Looper.java:158)                                                                   在android.app.ActivityThread.main(ActivityThread.java:7231)                                                                   at java.lang.reflect.Method.invoke(Native Method)                                                                   在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1230)                                                                   在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

当打字然后击退退格时,似乎发生了崩溃。

0 个答案:

没有答案