gpt4 book ai didi

java - 从书签中删除位置时出错

转载 作者:行者123 更新时间:2023-12-01 09:34:57 29 4
gpt4 key购买 nike

我的代码出现错误:

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)

此错误发生在该 fragment 代码中

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);
}

最佳答案

由于 bookmarkstemp_bookmarks 是同一对象的两个引用,因此当您从 temp_bookmarks 中删除项目时,您也会从 >书签,迭代此集合时不允许这样做。在迭代之前尝试从书签创建一个新集合:

Set<String> temp_bookmarks = new HashSet<>(bookmarks);

这样,您迭代 temp_bookmarks 时,原始 Set 将保持不变,这很好。

旁注:在 Java 中处理变量时,建议使用 camelCase 命名约定,因此我建议您将名称 temp_bookmarks 更改为 tempBookmarks,并使用代码中的其他变量和方法来执行此操作。

关于java - 从书签中删除位置时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39072310/

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