【发布时间】:2015-06-16 10:35:47
【问题描述】:
当我使用循环删除RecyclerView 中的某些项目时,为什么会执行异常?
我在适配器中使用了Collentions.synchronizedMap,'deleteItem 方法'也使用了同步(片段中的方法)。
public void elementController(JsonObject jsonObject , String type) {
if ( jsonObject == null || type == null ) {
return;
}
int position =0 , resultPosition =0;
if ( type.equals("update") || type.equals("delete")) {
String id = jsonObject.get(ELEMENT_ID).getAsString();
Map<String , Element> map = gridFragment.getMap();
synchronized (map) {
for (String s : map.keySet()) {
if (s.equals(id)) {
resultPosition = position;
} else {
position++;
}
}
}
}
if(position-1 > gridFragment.getmAdapter().getData().size() || position <0) {
return;
}
switch (type) {
case "add":
if (gridFragment.addElement(MyJsonParser.ElementParse(jsonObject),0)){
LogUtils.logDebug(TAG,"add end");
}
break;
case "update":
if(gridFragment.updateElement( updateParser(jsonObject),resultPosition)){
LogUtils.logDebug(TAG,"update end");
}
break;
case "delete":
if(gridFragment.deleteElement(jsonObject.get(ELEMENT_ID).getAsString(),resultPosition)){
LogUtils.logDebug(TAG,"delete end");
}
break;
}
}
public boolean deleteElement(final String id , final int position){
new Thread(new Runnable() {
@Override
public void run() {
getActivity().runOnUiThread(new Runnable(){
@Override
public void run() {
synchronized (map) {
map.remove(id);
mAdapter.setData(map);
mAdapter.notifyItemRemoved(position);
}
}
});
}
}).start();
return true;
}
我的错误日志:
java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid item position 0(offset:0).state:4
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:3382)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:3340)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1810)
at android.support.v7.widget.GridLayoutManager.layoutChunk(GridLayoutManager.java:356)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1269)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:523)
at android.support.v7.widget.GridLayoutManager.onLayoutChildren(GridLayoutManager.java:151)
at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:1942)
at android.support.v7.widget.RecyclerView.resumeRequestLayout(RecyclerView.java:1171)
at android.support.v7.widget.RecyclerView$1.run(RecyclerView.java:167)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
at android.view.Choreographer.doCallbacks(Choreographer.java:574)
at android.view.Choreographer.doFrame(Choreographer.java:543)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:212)
at android.app.ActivityThread.main(ActivityThread.java:5137)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:902)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:718)
at dalvik.system.NativeStart.main(Native Method)
找不到设备
【问题讨论】:
-
第一个代码没有意义,你循环并且只从循环中获得一个位置。无中断条件
-
您在移除项目后设置 mAdapter 数据,然后尝试移除指定位置,在这种情况下该位置不存在。
-
服务调用的所有第一个代码(我使用带有服务器的套接字网络)。我从服务器获取 jsonarray 并通过 for 循环将 jsonobjects 发送到 jsonarray 中的第一个代码。所以这是第一个循环。我猜第二个 jsonobject 在结束第一个代码之前由 params 发送到第一个代码。
-
我添加了我所有的第一个方法。
标签: android indexoutofboundsexception synchronized android-recyclerview