我的代码出错:
java.util.ConcurrentModificationException
at java.util.HashMap$HashIterator.nextEntry(HashMap.java:787)
at java.util.HashMap$KeyIterator.next(HashMap.java:814)
at openprojects.atvt.ui.VideoAdapter.getLiveTvBookmarks(VideoAdapter.java:107)
at openprojects.atvt.ui.MainActivity.switchRowsFragment(MainActivity.java:323)
at openprojects.atvt.ui.MainActivity.setPosition(MainActivity.java:378)
at openprojects.atvt.ui.MainActivity.access$500(MainActivity.java:48)
at openprojects.atvt.ui.MainActivity$4.onRequestChildFocus(MainActivity.java:268)
at android.support.v17.leanback.widget.BrowseFrameLayout.requestChildFocus(BrowseFrameLayout.java:124)
at android.view.ViewGroup.requestChildFocus(ViewGroup.java:740)
at android.view.ViewGroup.requestChildFocus(ViewGroup.java:740)
at android.view.ViewGroup.requestChildFocus(ViewGroup.java:740)
at android.support.v7.widget.RecyclerView.requestChildFocus(RecyclerView.java:2106)
at android.view.ViewGroup.requestChildFocus(ViewGroup.java:740)
at android.view.View.handleFocusGainInternal(View.java:5471)
at android.view.View.requestFocusNoSearch(View.java:8476)
at android.view.View.requestFocus(View.java:8455)
at android.view.ViewRootImpl$ViewPostImeInputStage.processKeyEvent(ViewRootImpl.java:4203)
at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4089)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3642)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3695)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3661)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3787)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3669)
at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3844)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3642)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3695)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3661)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3669)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3642)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3695)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3661)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3820)
at android.view.ViewRootImpl$ImeInputStage.onFinishedInputEvent(ViewRootImpl.java:3981)
at android.view.inputmethod.InputMethodManager$PendingEvent.run(InputMethodManager.java:2253)
at android.view.inputmethod.InputMethodManager.invokeFinishedInputEventCallback(InputMethodManager.java:1874)
at android.view.inputmethod.InputMethodManager.finishedInputEvent(InputMethodManager.java:1865)
at android.view.inputmethod.InputMethodManager$ImeInputEventSender.onInputEventFinished(InputMethodManager.java:2230)
at android.view.InputEventSender.dispatchInputEventFinished(InputEventSender.java:141)
at android.os.MessageQueue.nativePollOnce(Native Method)
at android.os.MessageQueue.next(MessageQueue.java:323)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
此错误发生在此片段代码中
public List<LiveTvProgram> getLiveTvBookmarks() {
String name = context.getString(R.string.preferences_bookmarks_live_tv);
Set<String> bookmarks = preferenceHelper.getStringSet(name, name);
Set<String> temp_bookmarks = bookmarks;
ArrayList<LiveTvProgram> bookmarkList = new ArrayList<>();
for (String title : bookmarks) {
for (LiveTvProgram program : ChannelsManager.getInstance().getSelectedPrograms()) {
if (program.getTitle().equals(title)) {
if (program.hasEnded()) {
temp_bookmarks.remove(title);
} else {
bookmarkList.add(program);
}
break;
}
}
}
if (temp_bookmarks.size() != bookmarks.size()) {
preferenceHelper.putStringSet(name, name, temp_bookmarks);
}
return bookmarkList;
}
这就是我得到它的原因。当我添加名为onAIR的LiveProgram(目前在线播放)时,可以。我可以去书签部分,但是当程序结束时,必须从书签中删除,但我得到这样的错误。
我收到了错误:
if (program.hasEnded()) {
temp_bookmarks.remove(title);
} else {
bookmarkList.add(program);
}
答案 0 :(得分:1)
由于bookmarks
和temp_bookmarks
是同一对象的两个引用,因此当您从temp_bookmarks
中删除项目时,也会将其从bookmarks
中删除,这是不允许的迭代这个集合。在迭代之前尝试从bookmarks
创建一个新集合:
Set<String> temp_bookmarks = new HashSet<>(bookmarks);
通过这种方式,您迭代temp_bookmarks
,原始Set
将保持不变,这很不错。
旁注:在Java中使用变量时,建议使用camelCase
命名约定,因此我建议您将名称temp_bookmarks
更改为tempBookmarks
,并将其更改为使用代码中的其他变量和方法执行此操作。