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.

ClassCastException when the library call item's `onBindViewHolder`

See original GitHub issue

This is my first try on the library. I thought this is very awesome library, thank you. I try to implement a sectionable recycler-view, spent 2 days to read through wiki and the example but can’t fix the issue myself.
I initialize the adapter with null data-list then add one by one later. If I remove the setting setDisplayHeadersAtStartUp(true), the recycler works without header. It shows data-item only. If I add this setting, the app crash with ClassCastException. Please help to take a look. Thanks

upcomingAdapter = new FlexibleAdapter<>(null);
recyclerView.setAdapter(upcomingAdapter);
 upcomingAdapter.setDisplayHeadersAtStartUp(true);

//On data loading callback:
MyHeader headerItem = new MyHeader(task.deadlines.get(0).date);
MyItem item = new MyItem(task, headerItem);
upcomingAdapter.addItem(item);
   public class MyHeader extends AbstractHeaderItem<MyHeader.HeaderVH> {
        public final ObservableLong date = new ObservableLong();

        MyHeader(long date) {
            this.date.set(date);
            this.setHidden(false);
        }

        @Override
        public HeaderVH createViewHolder(FlexibleAdapter adapter, LayoutInflater inflater, ViewGroup parent) {
            View view = inflater.inflate(R.layout.view_upcoming_task_header_item, parent, false);
            return new HeaderVH(view, adapter);
        }

        @Override
        public void bindViewHolder(FlexibleAdapter adapter, HeaderVH holder, int position, List payloads) {

        }

        @Override
        public boolean equals(Object o) {
            if (o instanceof MyHeader) {
                return ((MyHeader) o).date.get() == this.date.get();
            }
            return false;
        }

        public class HeaderVH extends FlexibleViewHolder {
            public HeaderVH(View view, FlexibleAdapter adapter) {
                super(view, adapter, true);
            }

        }
    }

    class MyItem extends AbstractSectionableItem<MyItem.ItemVH, MyHeader> {
        Task task;

        MyItem(Task task, MyHeader header) {
            super(header);
            this.task = task;
        }

        @Override
        public boolean equals(Object o) {
            return false;
        }

        @Override
        public ItemVH createViewHolder(FlexibleAdapter adapter, LayoutInflater inflater, ViewGroup parent) {
            ViewUpcomingTaskItemBinding binding = DataBindingUtil.inflate(inflater, R.layout.view_upcoming_task_item, parent, false);
            return new ItemVH(binding, adapter);
        }

        @Override
        public void bindViewHolder(FlexibleAdapter adapter, ItemVH holder, int position, List payloads) {
            holder.bindData(task);
        }


        class ItemVH extends FlexibleViewHolder {
            ViewUpcomingTaskItemBinding mBinding;

            public ItemVH(ViewUpcomingTaskItemBinding binding, FlexibleAdapter adapter) {
                super(binding.getRoot(), adapter);
                mBinding = binding;
            }

            void bindData(Task dataItem) {
                mBinding.setTask(dataItem);
            }
        }
    }

Here is the logcat:

 java.lang.ClassCastException: com.makeit.lite.main.upcoming.UpcomingFragment$UpcomingHeaderItem$HeaderVH cannot be cast to com.makeit.lite.main.upcoming.UpcomingFragment$UpcomingItem$ItemVH
at com.makeit.lite.main.upcoming.UpcomingFragment$UpcomingItem.bindViewHolder(UpcomingFragment.java:166)
at eu.davidea.flexibleadapter.FlexibleAdapter.onBindViewHolder(FlexibleAdapter.java:1867)
at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:6341)
at android.support.v7.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:5287)
at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5550)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5392)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5388)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2149)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1533)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1496)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:593)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3535)
at android.support.v7.widget.RecyclerView.onMeasure(RecyclerView.java:2979)
at android.view.View.measure(View.java:18788)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.view.View.measure(View.java:18788)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
at android.view.View.measure(View.java:18788)
at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1658)
at android.view.View.measure(View.java:18788)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:715)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
at android.view.View.measure(View.java:18788)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139)
at android.view.View.measure(View.java:18788)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.support.v7.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:393)
at android.view.View.measure(View.java:18788)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.view.View.measure(View.java:18788)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
at android.view.View.measure(View.java:18788)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at com.android.internal.policy.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2643)
at android.view.View.measure(View.java:18788)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2100)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1216)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1452)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6013)
                                                                 	at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858
02-27 01:45:30.314 28658-28672/com.makeit.lite I/art: Starting a blocking GC Explicit
02-27 01:45:30.321 28658-28672/com.makeit.lite I/art: Explicit concurrent mark sweep GC freed 23301(1736KB) AllocSpace objects, 2(32KB) LOS objects, 24% free, 5MB/7MB, paused 106us total 6.685ms
02-27 01:50:27.232 28658-28658/? I/Process: Sending signal. PID: 28658 SIG: 9

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
davideascommented, Apr 6, 2017

@IHNEL, so this method should be declared abstract (maybe).

0reactions
IHNELcommented, Apr 6, 2017

Exactly what I mean. This approach will help to save lot of time for who may run into similar case.

Read more comments on GitHub >

github_iconTop Results From Across the Web

java - Recyclerview ClassCastException at onBindViewHolder
I made RecyclerView inside RecyclerView. There is ClassCastException at onBindViewHolder. The error message here. java.lang.
Read more >
Java.lang.ClassCastException casting issue while ... - GitHub
I have been using this library for over 3 months now. Went through a lot of libraries for Recyclerview Sticky Headers before finding...
Read more >
Using the RecyclerView | CodePath Android Cliffnotes
Every adapter has three primary methods: onCreateViewHolder to inflate the item layout and create the holder, onBindViewHolder to set the view attributes based ......
Read more >
B4A Library [Lib, Chargeable] Amir_RecyclerView - B4X
New b4a library! :) RecyclerView, an important part of the sdk and android for making a custom list with better Layout , animation...
Read more >
Fixing ClassCastException - YouTube
A ClassCastException occurs when you attempt to cast an object to a variable type that does not represent it. In this video, we...
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