RecyclerView 和 java.lang.IndexOutOfBoundsException 无效的视图持有者适配器 positionViewHolder
Posted
技术标签:
【中文标题】RecyclerView 和 java.lang.IndexOutOfBoundsException 无效的视图持有者适配器 positionViewHolder【英文标题】:RecyclerView and java.lang.IndexOutOfBoundsException Invalid view holder adapter positionViewHolder 【发布时间】:2017-10-04 10:27:55 【问题描述】:我有一个绑定杂货项目的RecyclerView
。添加项目完美。但是,当我尝试删除该项目时,应用程序崩溃并出现IndexOutOfBoundsException
错误。
我面临的问题在于我的onBindViewHolder()
。我试图获得适配器的位置,但这并没有解决问题。我也刚刚尝试使用notifyDataSetChanged()
,这只是删除了我的整个列表。
我的问题是我做错了什么,我该如何解决这个问题?
这是适配器类:
private class GroceryAdapter extends RecyclerView.Adapter<GroceryHolder>
private List<Grocery> groceries;
private GroceryHolder holder;
public GroceryAdapter()
setGroceries(GroceryList.get(GroceryActivity.this).getGroceries());
public void setGroceries(List<Grocery> groceries)
this.groceries = groceries;
@Override
public GroceryHolder onCreateViewHolder(ViewGroup parent, int viewType)
LayoutInflater layoutInflater= LayoutInflater.from(GroceryActivity.this);
View view=layoutInflater.inflate(R.layout.grocery_list_row, parent, false);
holder =new GroceryHolder(view);
return holder;
@Override
public void onBindViewHolder(final GroceryHolder holder, final int position)
final Grocery grocery = groceries.get(position);
holder.bindGrocery(grocery);
//private Grocery grocery;
holder.deleteImageButton.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
GroceryList groceryList = GroceryList.get(getApplicationContext());
groceryList.deleteGrocery(grocery);
groceryAdapter.setGroceries(GroceryList.get(getApplicationContext()).getGroceries());
//holder.getAdapterPosition();
groceryAdapter.notifyItemRemoved(position);
groceryAdapter.notifyItemRangeRemoved(position, groceries.size());
Toast.makeText(getApplicationContext(),"Item deleted", Toast.LENGTH_SHORT).show();
//finish();
);
@Override
public int getItemCount()
return groceries.size();
这里是日志
E/androidRuntime: FATAL EXCEPTION: main
java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter positionViewHolder426a0900 position=0 id=-1, oldPos=1, pLpos:1 scrap [attachedScrap] tmpDetached no parent
at android.support.v7.widget.RecyclerView$Recycler.validateViewHolderForOffsetPosition(RecyclerView.java:5297)
at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5479)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5440)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5436)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2224)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1551)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1511)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:595)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep1(RecyclerView.java:3534)
at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3310)
at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3844)
at android.view.View.layout(View.java:15745)
at android.view.ViewGroup.layout(ViewGroup.java:4867)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1677)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1531)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1440)
at android.view.View.layout(View.java:15745)
at android.view.ViewGroup.layout(ViewGroup.java:4867)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
at android.view.View.layout(View.java:15745)
at android.view.ViewGroup.layout(ViewGroup.java:4867)
at android.support.v7.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:437)
at android.view.View.layout(View.java:15745)
at android.view.ViewGroup.layout(ViewGroup.java:4867)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
at android.view.View.layout(View.java:15745)
at android.view.ViewGroup.layout(ViewGroup.java:4867)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1677)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1531)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1440)
at android.view.View.layout(View.java:15745)
at android.view.ViewGroup.layout(ViewGroup.java:4867)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
at android.view.View.layout(View.java:15745)
at android.view.ViewGroup.layout(ViewGroup.java:4867)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2356)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2069)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1254)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6624)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:812)
at android.view.Choreographer.doCallbacks(Choreographer.java:612)
at android.view.Choreographer.doFrame(Choreographer.java:582)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:798)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5602)
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:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method)
【问题讨论】:
您能否解释一下为什么在您的Adapter
类中需要 groceryAdapter
以及您在哪里创建该实例?
【参考方案1】:
您不应将final int position
传递给您的匿名new View.OnClickListener()
改为使用holder.getAdapterPosition()
:
private class GroceryAdapter extends RecyclerView.Adapter<GroceryHolder>
private List<Grocery> groceries;
public GroceryAdapter(List<Grocery> groceries)
this.groceries = groceries;
@Override
public GroceryHolder onCreateViewHolder(ViewGroup parent, int viewType)
LayoutInflater layoutInflater= LayoutInflater.from(GroceryActivity.this);
View view=layoutInflater.inflate(R.layout.grocery_list_row, parent, false);
holder = new GroceryHolder(view);
return holder;
@Override
public void onBindViewHolder(final GroceryHolder holder, int position)
int safePosition = holder.getAdapterPosition();
final Grocery grocery = groceries.get(safePosition);
holder.bindGrocery(grocery);
holder.deleteImageButton.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
// delete item from the list
groceries.remove(safePosition);
groceryAdapter.notifyItemRemoved(safePosition);
Toast.makeText(getApplicationContext(),"Item deleted", Toast.LENGTH_SHORT).show();
);
@Override
public int getItemCount()
return groceries.size();
另外,还有几个不相关的问题:
最好不要在onBindViewHolder
中设置setOnClickListener
,而是在onCreateViewHolder
中设置onCreateViewHolder
在构造函数中设置杂货列表的代码看起来太复杂了。将此列表作为 Activity/Fragment 的参数传递可能会更简单。
private GroceryHolder holder
作为实例变量是不必要的
这里有更多关于holder.getAdapterPosition()
https://youtu.be/imsr8NrIAMs?t=2092的上下文
【讨论】:
以上是关于RecyclerView 和 java.lang.IndexOutOfBoundsException 无效的视图持有者适配器 positionViewHolder的主要内容,如果未能解决你的问题,请参考以下文章
RecyclerView 和 java.lang.IndexOutOfBoundsException:检测到不一致。三星设备中的无效视图支架适配器 positionViewHolder
java.lang.IllegalStateException:RecyclerView 在 Fragment 中没有 LayoutManager
Eclipse中使用recyclerview时出现Caused by: java.lang.NoClassDefFoundError: android.support.v7.recyclerview.
RecyclerView ViewPager java.lang.NullPointerException android.support.v7.widget.RecyclerView$ItemAni
java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 in recyclerview
java.lang.IndexOutOfBoundsException :尝试从 RecyclerView 中删除 URL 损坏的项目