Recycler View Fatal Exception: java.lang.ArrayIndexOutOfBoundsException

I have this stack trace though starchy. I do not know where the problem is.

Is there an alternative to StaggeredGridLayoutManager that I can use to get a list view.

Fatal Exception: java.lang.ArrayIndexOutOfBoundsException: start < 0 || end > len. start=-1, end=11, len=11 at java.util.Arrays.checkStartAndEnd(Arrays.java:1745) at java.util.Arrays.fill(Arrays.java:803) at android.support.v7.widget.StaggeredGridLayoutManager$LazySpanLookup.invalidateAfter(SourceFile:2404) at android.support.v7.widget.StaggeredGridLayoutManager.handleUpdate(SourceFile:1373) at android.support.v7.widget.StaggeredGridLayoutManager.onItemsRemoved(SourceFile:1327) at android.support.v7.widget.RecyclerView$6.dispatchUpdate(SourceFile:725) at android.support.v7.widget.RecyclerView$6.onDispatchFirstPass(SourceFile:716) at android.support.v7.widget.AdapterHelper.dispatchFirstPassAndUpdateViewHolders(SourceFile:316) at android.support.v7.widget.AdapterHelper.dispatchAndUpdateViewHolders(SourceFile:302) at android.support.v7.widget.AdapterHelper.applyRemove(SourceFile:182) at android.support.v7.widget.AdapterHelper.preProcess(SourceFile:103) at android.support.v7.widget.RecyclerView.processAdapterUpdatesAndSetAnimationFlags(SourceFile:2767) at android.support.v7.widget.RecyclerView.onMeasure(SourceFile:2538) at android.view.View.measure(View.java:16521) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5134) at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404) at android.widget.LinearLayout.measureVertical(LinearLayout.java:695) at android.widget.LinearLayout.onMeasure(LinearLayout.java:588) at android.view.View.measure(View.java:16521) at android.support.v4.view.ViewPager.onMeasure(SourceFile:1489) at android.view.View.measure(View.java:16521) at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:719) at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:455) at android.view.View.measure(View.java:16521) at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:719) at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:455) at android.view.View.measure(View.java:16521) at android.support.v4.widget.DrawerLayout.onMeasure(SourceFile:940) at android.view.View.measure(View.java:16521) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5134) at android.widget.FrameLayout.onMeasure(FrameLayout.java:310) at android.support.v7.widget.ContentFrameLayout.onMeasure(SourceFile:135) at android.view.View.measure(View.java:16521) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5134) at android.widget.FrameLayout.onMeasure(FrameLayout.java:310) at android.view.View.measure(View.java:16521) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5134) at android.widget.FrameLayout.onMeasure(FrameLayout.java:310) at android.view.View.measure(View.java:16521) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5134) at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404) at android.widget.LinearLayout.measureVertical(LinearLayout.java:695) at android.widget.LinearLayout.onMeasure(LinearLayout.java:588) at android.view.View.measure(View.java:16521) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5134) at android.widget.FrameLayout.onMeasure(FrameLayout.java:310) at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2331) at android.view.View.measure(View.java:16521) at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1925) at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1117) at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1299) at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004) at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5739) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761) at android.view.Choreographer.doCallbacks(Choreographer.java:574) at android.view.Choreographer.doFrame(Choreographer.java:544) 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:136) at android.app.ActivityThread.main(ActivityThread.java:5380) at java.lang.reflect.Method.invokeNative(Method.java) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:970) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:786) at dalvik.system.NativeStart.main(NativeStart.java) 

This is what I do for click processing in one of the adapters

  @Override public void onBindViewHolder(final ViewHolder holder, final int position) { holder.relative.setClickable(true); holder.relative.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (itemClickListener != null) itemClickListener.onClick(v, position); else AppConfig.log("itemClickLestener was null"); } }); holder.relative.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View v) { if (itemClickListener != null) itemClickListener.onLongClick(v, position); return true; } }); return view; } 
+5
source share
2 answers

It looks like you tried to call getAdapterPosition () , while the Recycler View layouts are not evaluated (possibly caused by the call to notifyDataSetChanged ()).

always treat it like this:

 final int position = getAdapterPosition(); if(positon != RecyclerView.NO_POSITION){ // you can trust the adapter position // do whatever you intend to do with this position } 

The documentation clearly states the following

if you want to access the element in the adapter by clicking ViewHolder, you must use getAdapterPosition (). Remember that these methods cannot calculate adapter positions if notifyDataSetChanged () has been called and a new layout has not yet been calculated. For this reason, you should carefully process the NO_POSITION or null results of these methods.

UPDATE

Now that you have finally posted the code. Here are some things you should consider.

  • Do not skip the final parameters inside onBindViewHolder. By the time OnClickListener is fired, the position is not guaranteed at the same level as you think. So do it

     @Override public void onBindViewHolder(ViewHolder holder, int position) { holder.relative.setClickable(true); //do other stuff } 
  • DO NOT assign listeners inside the onBindViewHolder method. As the view is refined, you end up setting up listeners several times to the same look. Instead, set it inside the view holder class or onCreateViewHolder method, as shown below

     @Override public MyViewHolder onCreateViewHolder (ViewGroup parent, int viewType) { View myView = LayoutInflater.from(parent.getContext()) .inflate(R.layout.my_layout, parent, false); //setting listeners on creation myView.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) { final int position = getAdapterPosition(); if(position != RecyclerView.NO_POSITION){ //do whatever you want with the clicked position } } }); //return the view holder return new MyViewHolder(myView); } 

    Or in the ViewHolder class

     public class MyViewHolder extends RecyclerView.ViewHolder { public MyViewHolder(View itemView) { super(itemView); //setting listeners inside viewHolder class itemView.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) { final int position = getAdapterPosition(); if(position != RecyclerView.NO_POSITION){ //do whatever you want with the clicked position } } }); } } 

Fixing the above may solve your problem. I highly recommend that you watch the 2015 Google Dev Summit 2015 RecyclerView video. This is really interesting, and they talk about RecyclerView errors. They also mention why not pass final parameters inside onBinViewHolder. Click here to find out the exact time in the video in which they talk about it.

+16
source

Basically this error will occur in your bindview, where you will use the data of your arraylist. Make sure you use position -1 of the arraylist value.

0
source

Source: https://habr.com/ru/post/1243657/


All Articles