当我尝试使用this库处理分段的recyclerview适配器时,我一直收到此错误。
现在这里是我正在获得的整个堆栈跟踪:
E/UncaughtException: java.util.ConcurrentModificationException
at java.util.HashMap$HashIterator.nextEntry(HashMap.java:851)
at java.util.HashMap$EntryIterator.next(HashMap.java:891)
at java.util.HashMap$EntryIterator.next(HashMap.java:890)
at io.github.luizgrp.sectionedrecyclerviewadapter.SectionedRecyclerViewAdapter.onCreateViewHolder(SectionedRecyclerViewAdapter.java:42)
at android.support.v7.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:6290)
at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5478)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5363)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5359)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2141)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1525)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1488)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:585)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3506)
at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3260)
at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3767)
at android.view.View.layout(View.java:18793)
at android.view.ViewGroup.layout(ViewGroup.java:5952)
at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1079)
at android.view.View.layout(View.java:18793)
at android.view.ViewGroup.layout(ViewGroup.java:5952)
at android.support.v4.view.ViewPager.onLayout(ViewPager.java:1795)
at android.view.View.layout(View.java:18793)
at android.view.ViewGroup.layout(ViewGroup.java:5952)
at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1079)
at android.view.View.layout(View.java:18793)
at android.view.ViewGroup.layout(ViewGroup.java:5952)
at android.support.design.widget.CoordinatorLayout.layoutChild(CoordinatorLayout.java:1167)
at android.support.design.widget.CoordinatorLayout.onLayoutChild(CoordinatorLayout.java:852)
at android.support.design.widget.ViewOffsetBehavior.layoutChild(ViewOffsetBehavior.java:63)
at android.support.design.widget.HeaderScrollingViewBehavior.layoutChild(HeaderScrollingViewBehavior.java:135)
at android.support.design.widget.ViewOffsetBehavior.onLayoutChild(ViewOffsetBehavior.java:42)
at android.support.design.widget.AppBarLayout$ScrollingViewBehavior.onLayoutChild(AppBarLayout.java:1375)
at android.support.design.widget.CoordinatorLayout.onLayout(CoordinatorLayout.java:870)
at android.view.View.layout(View.java:18793)
at android.view.ViewGroup.layout(ViewGroup.java:5952)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:18793)
at android.view.ViewGroup.layout(ViewGroup.java:5952)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1494)
at android.view.View.layout(View.java:18793)
at android.view.ViewGroup.layout(ViewGroup.java:5952)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:18793)
at android.view.ViewGroup.layout(ViewGroup.java:5952)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1494)
at android.view.View.layout(View.java:18793)
at android.view.ViewGroup.layout(ViewGroup.java:5952)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
这是他的库中产生错误的代码的一部分:
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
RecyclerView.ViewHolder viewHolder = null;
View view = null;
for (Map.Entry<String, Integer> entry : sectionViewTypeNumbers.entrySet()) {
if (viewType >= entry.getValue() && viewType < entry.getValue() + VIEW_TYPE_QTY) {
Section section = sections.get(entry.getKey());
int sectionViewType = viewType - entry.getValue();
switch (sectionViewType) {
case VIEW_TYPE_HEADER: {
Integer resId = section.getHeaderResourceId();
if (resId == null)
throw new NullPointerException("Missing 'header' resource id");
view = LayoutInflater.from(parent.getContext()).inflate(resId, parent, false);
// get the header viewholder from the section
viewHolder = section.getHeaderViewHolder(view);
break;
}
case VIEW_TYPE_FOOTER: {
Integer resId = section.getFooterResourceId();
if (resId == null)
throw new NullPointerException("Missing 'footer' resource id");
view = LayoutInflater.from(parent.getContext()).inflate(resId, parent, false);
// get the footer viewholder from the section
viewHolder = section.getFooterViewHolder(view);
break;
}
case VIEW_TYPE_ITEM_LOADED: {
view = LayoutInflater.from(parent.getContext()).inflate(section.getItemResourceId(), parent, false);
// get the item viewholder from the section
viewHolder = section.getItemViewHolder(view);
break;
}
case VIEW_TYPE_LOADING: {
Integer resId = section.getLoadingResourceId();
if (resId == null) throw new NullPointerException("Missing 'loading state' resource id");
view = LayoutInflater.from(parent.getContext()).inflate(resId, parent, false);
// get the loading viewholder from the section
viewHolder = section.getLoadingViewHolder(view);
break;
}
case VIEW_TYPE_FAILED: {
Integer resId = section.getFailedResourceId();
if (resId == null) throw new NullPointerException("Missing 'failed state' resource id");
view = LayoutInflater.from(parent.getContext()).inflate(resId, parent, false);
// get the failed load viewholder from the section
viewHolder = section.getFailedViewHolder(view);
break;
}
default:
throw new IllegalArgumentException("Invalid viewType");
}
}
}
return viewHolder;
}
最后我的部分是从服务器数据库中获取数据并在部分内部显示recyclelerview。在某个地方,我犯了一个错误,希望有人能指出我正确的方向。所以这就是:
if (!response.getBoolean("error")) {
dates.clear();
originalList.clear();
mEvents.clear();
mSectionedRecyclerViewAdapter.removeAllSections();
mSectionedRecyclerViewAdapter.notifyDataSetChanged();
JSONArray eventsArray = response.getJSONArray("events");
for (int i = 0; i < eventsArray.length(); i++) {
JSONObject eventObj = eventsArray.getJSONObject(i);
Event event = new Event();
if (eventObj.getInt("field_id") != 0) {
Field field = new Field();
field.setId(eventObj.getInt("field_id"));
field.setAddress(eventObj.getString("field_address"));
field.setName(eventObj.getString("field_name"));
if (!eventObj.isNull("field_contact")) {
field.setMobileNumberFirst(eventObj.getString("field_contact"));
}
if (!eventObj.isNull("score")) {
field.setReviewScore(eventObj.getDouble("score"));
field.setReviewNum(eventObj.getInt("review_num"));
}
event.setField(field);
location = event.getField().getAddress();
event.setLocation(location);
} else {
event.setLocation(eventObj.getString("event_location"));
location = eventObj.getString("event_location");
}
double distanceInKm = Double.parseDouble(eventObj.getString("distance_km"));
int distanceInMet = eventObj.getInt("distance_m");
if (distanceInKm >= 1) {
event.setDistance(distanceInKm + " km");
} else {
event.setDistance(distanceInMet + " m");
}
dates.add(event.getDatetime().substring(0, 10));
mEvents.add(event);
}
// add elements to al, including duplicates
Set<String> hs = new LinkedHashSet<>();
hs.addAll(dates);
dates.clear();
dates.addAll(hs);
Collections.sort(dates, new Comparator<String>() {
@Override
public int compare(String s, String t1) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try {
return sdf.parse(s).compareTo(sdf.parse(t1));
} catch (ParseException e) {
e.printStackTrace();
}
return 0;
}
});
for (String date : dates) {
originalList = getEventsWithDate(date);
if (formattedDate.equals(date)) {
date = "Danas";
} else {
int monthOfYear = 0;
Date startDate;
try {
startDate = myDateFormat.parse(date);
Calendar calendar = Calendar.getInstance();
calendar.setTime(startDate);
monthOfYear = calendar.get(Calendar.MONTH);
} catch (ParseException e) {
e.printStackTrace();
}
String month = getMonth(monthOfYear);
try {
String monthNum = date.substring(8, 10);
date = monthNum + " " + month;
} catch (Exception ignored) {
}
}
if (originalList.size() > 0) {
Collections.sort(originalList, new Comparator<Event>() {
@Override
public int compare(Event event, Event t1) {
return event.getDateFromString().compareTo(t1.getDateFromString());
}
});
if (matchType == 3) {
mSectionedRecyclerViewAdapter.addSection(new EventSection(R.layout.lst_item_event_v4, date, originalList, getActivity(), matchType));
} else {
mSectionedRecyclerViewAdapter.addSection(new EventSection(R.layout.lst_item_event_v2, date, originalList, getActivity(), matchType));
}
}
}
}