Bug:java.lang.IllegalStateException
使用迭代的时候,出现了java.lang.IllegalStateException
代码:
1 for ( TaskInfo info : userTaskInfos ) { 2 if ( info.isChecked() ) { 3 am.killBackgroundProcesses(info.getPackageName()); 4 if ( getPackageName().equals(info.getPackageName()) ) { 5 continue; 6 } 7 avaliMem = avaliMem + info.getMemSize(); 8 runningTaskCount = runningTaskCount - 1; 9 userTaskInfos.remove(info) 10 11 } 12 }
原因:迭代的时候,不能操作数据。
解决办法:
1 List< TaskInfo > removeTasks = new ArrayList<>(); 2 3 for ( TaskInfo info : userTaskInfos ) { 4 if ( info.isChecked() ) { 5 am.killBackgroundProcesses(info.getPackageName()); 6 if ( getPackageName().equals(info.getPackageName()) ) { 7 continue; 8 } 9 avaliMem = avaliMem + info.getMemSize(); 10 runningTaskCount = runningTaskCount - 1; 11 removeTasks.add(info); 12 } 13 } 14 userTaskInfos.removeAll(removeTasks);
直面挑战,躬身入局