question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter positionViewHolder

See original GitHub issue

Hi Davideas & developers,

I’m facing to a strange problem and I don’t know if I do something wrong with the FlexibleAdapter.

To explain the situation :

In a form, I can open different list of choices from some clicks on edit text view. To fill the list of choices, I’m using a content provider that gets back a cursor from database fulfilled with some HEADER type and SUBITEM type. Both are stored in a LIST of storage choice model.

This LIST is then used as input of my implementation of FlexibleAdapter.

At this step, I’m reading all the list content and create ExpandableHeaderItem if type is Header (with sticky feature activated) and SubItem is other… The process of creation is quite simple, see below

items.clear();   // flush old items
ExpandableHeaderItem currentHeader = null;
for (int i = 0; i < choiceModelList.size(); i++) {
      // HEADER
      if (choiceModelList.get(i).getItemType() == ChoiceModel.GROUP_TYPE) {
                    currentHeader = new ExpandableHeaderItem(choiceModelList.get(i));
                    items.add(currentHeader);
       } else if (choiceModelList.get(i).getItemType() == ChoiceModel.ITEM_TYPE) {
       // ITEM
                    SubItem subItem = new SubItem(choiceModelList.get(i), currentHeader);
                     if (currentHeader != null) {
                           currentHeader.addSubItem(subItem);
                     } else {
                           items.add(subItem);
                     }
        }
}

So I’m using ExpandableHeaderItem extending

AbstractModelItem<ExpandableHeaderItem.ExpandableHeaderViewHolder>
and SubItem extends AbstractModelItem<SubItem.ChildViewHolder>

And Sticky is initialized by

@Override
	public ViewGroup getStickySectionHeadersHolder() {
		FrameLayout frameLayout = new FrameLayout(mRecyclerView.getContext());
        frameLayout.setLayoutParams(new ViewGroup.LayoutParams(
				ViewGroup.LayoutParams.MATCH_PARENT,//or MATCH_PARENT
				ViewGroup.LayoutParams.WRAP_CONTENT));

        //This is important otherwise the Header disappears!
		((ViewGroup) mRecyclerView.getParent()).addView(frameLayout);
		return (ViewGroup) LayoutInflater.from(AproPlanApplication.getAppContext()).inflate(R.layout.sticky_header_layout, frameLayout);
	}

On content initialization all seems is perfect, I get my complete list of headers and subitems for each headers. And I can click on header and its content collapsed/uncollapsed -> Working pretty well !!! The same for sticky behavior, headers are pushing others even if expanded or collapsed.

So all works fine, I choose element and quit the list to open another with another content build exactly as the previous…from cursor, etc …

Then troubles begin, this time I click on the first or second sticky headers…and boom my app is crashing… But if there’s a third header…it collapsed normally.

The crash is due to :

UncaughtException: java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter positionViewHolder{d542b3a position=2 id=-1612110282, oldPos=2, pLpos:-1 scrap [attachedScrap] tmpDetached no parent} at android.support.v7.widget.RecyclerView$Recycler.validateViewHolderForOffsetPosition (RecyclerView.java:5249) at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline (RecyclerView.java:5431) at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition (RecyclerView.java:5392) at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition (RecyclerView.java:5388)… etc

So it’s seems I try to get a header item from my recyclerView that is not available anymore out of boundaries…

I also see this print on my log appearing sometimes when I scroll the list content with collapsed header content.

View: isRecyclable decremented below 0: unmatched pair of setIsRecyable() calls for ViewHolder{ade43eb position=3 id=-32702395, oldPos=-1, pLpos:-1 no parent}

I tried with or without Sticky activated. I tried with recyclerView.setHasFixedSize(true | false);

Do you already have this kind of problem ? Any hint will be appreciated …

Can you tell me if I need to use some initialization or refresh tips ? Manually add setIsRecyclable(true) or setIsRecyclable(false) somewhere…

Thanks in advance. Lo.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
lostationcommented, Feb 8, 2017

I found my mistake. I was not a question of copy of dataset neither notify* methods… In my dataset’s content sometimes I used same values as title in views and in my abstract model I was using a wrong implementation of “id”. I used to get the id and hash based on the title…but like I said sometimes this value was the same than another item in the content of the recycler… I was hashing this value based on the same value -> same hash… So recycler was fighting with problematic of id.

So to resolve that issue… I just added a unique id mechanism.

public AbstractModelItem() { this.id = UUID.randomUUID().toString(); }

so I’m ensure that my id is really unique !!! Now it’s perfectly working in folding/unfolding header content.

0reactions
davideascommented, Feb 8, 2017

Good, but still don’t call the methods notify**, let the adapter do it for you, check the code of the adapter so you understand.

Read more comments on GitHub >

github_iconTop Results From Across the Web

RecyclerView and java.lang.IndexOutOfBoundsException ...
RecyclerView and java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter positionViewHolder in Samsung devices.
Read more >
Inconsistency detected. Invalid view holder adapter ... - GitHub
I followed a basic usage tutorial and I got a exception: Process: com.example, PID: 13642 java.lang.IndexOutOfBoundsException: Inconsistency detected.
Read more >
Solution for RecyclerView — IndexOutOfBoundsException
If you are working with recycler views on Android, you might be encountering this dreaded error: IndexOutOfBoundsException — Inconsistency detected. Invalid ...
Read more >
RecyclerView doesn't properly clear state when setAdapter ...
java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter positionViewHolder{15796f40 position=-1 id=256279, oldPos=9, ...
Read more >
Android : RecyclerView and java.lang ... - YouTube
... and java. lang. IndexOutOfBoundsException : Inconsistency detected. Invalid view holder adapter positionViewHolder in Samsung devices [ B..
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found