rtugeek / MaterialBanner

MaterialBanner
https://github.com/rtugeek/MaterialBanner
81 stars 19 forks source link

IndexOutOfBoundsException in some device #4

Open superdiazzz opened 3 years ago

superdiazzz commented 3 years ago

I often got crash from internal library, however i can't handle it from outside. What make me so confused was i can't produce this crash. Please help me

Fatal Exception: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
       at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
       at java.util.ArrayList.get(ArrayList.java:308)
       at androidx.viewpager.widget.ViewPager.performDrag(ViewPager.java:2324)
       at androidx.viewpager.widget.ViewPager.onTouchEvent(ViewPager.java:2242)
       at com.freegeek.android.materialbanner.view.MaterialViewPager.onTouchEvent(MaterialViewPager.java:67)
       at android.view.View.dispatchTouchEvent(View.java:8072)
       at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2429)
       at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2153)
       at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2435)
       at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2168)
       at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2435)
       at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2168)
       at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2435)
       at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2168)
       at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2435)
       at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2168)
       at com.freegeek.android.materialbanner.MaterialBanner.dispatchTouchEvent(MaterialBanner.java:321)
       at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2435)
       at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2168)
       at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2435)
       at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2168)
       at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2435)
       at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2168)
       at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2435)
       at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2168)
       at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2435)
       at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2168)
       at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2435)
       at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2168)
       at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2435)
       at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2168)
       at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2435)
       at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2168)
       at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2435)
       at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2168)
       at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2435)
       at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2168)
       at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2435)
       at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2168)
       at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2435)
       at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2168)
       at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2435)
       at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2168)
       at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2435)
       at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2168)
       at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2435)
       at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2168)
       at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2435)
       at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2168)
       at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2435)
       at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2168)
       at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2435)
       at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2168)
       at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2309)
       at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1628)
       at android.app.Activity.dispatchTouchEvent(Activity.java:2580)
       at androidx.appcompat.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:69)
       at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2257)
       at android.view.View.dispatchPointerEvent(View.java:8271)
       at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4738)
       at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4602)
       at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4160)
       at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4214)
       at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4183)
       at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4294)
       at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4191)
       at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:4351)
       at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4160)
       at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4214)
       at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4183)
       at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4191)
       at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4160)
       at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:6543)
       at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:6456)
       at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:6427)
       at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:6392)
       at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:6623)
       at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
       at android.view.InputEventReceiver.nativeConsumeBatchedInputEvents(InputEventReceiver.java)
       at android.view.InputEventReceiver.consumeBatchedInputEvents(InputEventReceiver.java:176)
       at android.view.ViewRootImpl.doConsumeBatchedInput(ViewRootImpl.java:6596)
       at android.view.ViewRootImpl$ConsumeBatchedInputRunnable.run(ViewRootImpl.java:6642)
       at android.view.Choreographer$CallbackRecord.run(Choreographer.java:812)
       at android.view.Choreographer.doCallbacks(Choreographer.java:612)
       at android.view.Choreographer.doFrame(Choreographer.java:580)
       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:5641)
       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:1288)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1104)
       at dalvik.system.NativeStart.main(NativeStart.java)
rtugeek commented 3 years ago

Can you provide an example code that you use in the project?

superdiazzz commented 3 years ago

Hi @rtugeek , sorry for my late response. This is how i use the library, i put it on my Viewholder item_adapter.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/parent_headline_endless"
    android:animateLayoutChanges="true"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <FrameLayout
        android:id="@+id/ad_view_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">
    </FrameLayout>
    <!-- Headline -->

    <FrameLayout
        android:id="@+id/slide_parent"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:visibility="visible"
        app:layout_constraintDimensionRatio="w,5:8"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/ad_view_container">

        <com.freegeek.android.materialbanner.MaterialBanner
            android:id="@+id/banner_slider"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:visibility="visible" />

        <com.rd.PageIndicatorView
            android:id="@+id/page_indicator"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|center"
            android:layout_marginBottom="@dimen/_16dp"
            app:piv_animationType="drop"
            app:piv_dynamicCount="true"
            app:piv_interactiveAnimation="true"
            app:piv_padding="@dimen/_4dp"
            app:piv_radius="@dimen/_4dp"
            app:piv_selectedColor="@color/colorWhite"
            app:piv_unselectedColor="@color/colorGray" />
    </FrameLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/slide_parent">

        <!-- Trending Tag -->
        <HorizontalScrollView
            android:id="@+id/horizontal_tag_scroll"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:scrollbars="none"
            android:visibility="visible">

            <LinearLayout
                android:id="@+id/layout_for_tag"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:visibility="gone">
            </LinearLayout>

        </HorizontalScrollView>

        <HorizontalScrollView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:scrollbars="none"
            android:layout_margin="@dimen/_4dp">

            <LinearLayout
                android:id="@+id/layout_for_logo_and_weather"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:visibility="gone"
                android:orientation="horizontal">

                <include
                    android:id="@+id/headline_weather"
                    android:visibility="gone"
                    layout="@layout/item_weather" />
            </LinearLayout>
        </HorizontalScrollView>
    </LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

And my ViewHolder

class HeadlineEndlessViewHolder(itemView: View,
                                private val application: TribunNewsApplication,
                                private val activity: FragmentActivity,
                                private val ctx: Context,
                                private val kanal: String,
                                private val weather: Weather?,
                                private val imsakiyah: Imsakiyah?,
                                private val config: TribunConfig?,
                                private val dataPreference: DataPreference,
                                private val titleSize: Float
) : RecyclerView.ViewHolder(itemView), LifecycleObserver {

    private lateinit var adView: AdManagerAdView

    private val slider = itemView.findViewById<MaterialBanner<Any>>(R.id.banner_slider)
    private val indicator = itemView.findViewById<PageIndicatorView>(R.id.page_indicator)
    private val adContainer = itemView.findViewById<FrameLayout>(R.id.ad_view_container)

    var pageExist : Boolean = false
    private var initialLayoutComplete = false

    fun bindItem(headline: Headline?) {
        itemView.run {
            activity.lifecycle.addObserver(this@HeadlineEndlessViewHolder)
            headline?.let {
                if(!headline.headline.isNullOrEmpty()){
                    val news = headline.headline[0]
                    val vpager = slider.viewPager
                    indicator.setViewPager(vpager)

                    val headlines = headline.headline
                    headlines?.let {
                        if(headlines.isNotEmpty()){
                            pageExist = true
                            slider.setPages(EditSimpleHolderCreator(kanal, titleSize), headlines)
                            slider.setOnPageChangeListener(object : ViewPager.OnPageChangeListener{
                                override fun onPageScrollStateChanged(state: Int) {}
                                override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {}
                                override fun onPageSelected(position: Int) {
                                    indicator.setSelected(position)
                                }
                            })
                        }
                    }
                  -------------
                }
            }
        }
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
    fun connectPauseListener(){
        Timber.d("LIFECYCLE HEADLINE PAUSE")
        if(::adView.isInitialized){
            adView.pause()
        }
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
    fun connectDestroyListener(){
        Timber.d("LIFECYCLE HEADLINE DESTROY")
        stopSlide()
        if(::adView.isInitialized){
            adView.destroy()
        }
    }

    fun playSlide() {
        slider.startTurning(4000)
    }

    fun stopSlide(){
        slider.stopTurning()
    }
}