leandroBorgesFerreira / LoadingButtonAndroid

A button to substitute the ProgressDialog
MIT License
1.94k stars 214 forks source link

ClassCastException when applying a theme #130

Closed cdongieux closed 5 years ago

cdongieux commented 5 years ago

Hi,

I have a crash when a theme is applied to the CircularProgressButton in the XML declaration. The crash:

     Caused by: java.lang.ClassCastException: android.view.ContextThemeWrapper cannot be cast to androidx.lifecycle.LifecycleOwner
        at br.com.simplepass.loadingbutton.utils.UtilsKt.addLifecycleObserver(Utils.kt:53)
        at br.com.simplepass.loadingbutton.customViews.ProgressButtonKt.init(ProgressButton.kt:107)
        at br.com.simplepass.loadingbutton.customViews.ProgressButtonKt.init$default(ProgressButton.kt:77)
        at br.com.simplepass.loadingbutton.customViews.CircularProgressButton.<init>(CircularProgressButton.kt:28)

The XML extract:

    <br.com.simplepass.loadingbutton.customViews.CircularProgressButton
        android:id="@+id/pay_validate_button"
        android:theme="@style/PayDialogTheme"
        style="@style/Widget.MaterialComponents.Button"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toBottomOf="@id/pay_validate_coupon_layout"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        android:layout_marginStart="16dp"
        android:layout_marginEnd="16dp"
        android:layout_marginBottom="8dp"
        android:layout_gravity="center"
        android:background="@drawable/button_pay"
        android:text="@string/s_purchase_tunnel_payment_validation_button"
        android:textColor="@color/color_overlay_content_background"
        app:spinning_bar_color="@color/color_overlay_content_background"
        app:spinning_bar_padding="5dp" />

Here is the theme:

    <style name="PayDialogTheme" parent="Theme.MaterialComponents.Light.BottomSheetDialog">
        <item name="colorAccent">@color/colorAccent</item>
        <item name="colorPrimary">@color/color_overlay_text2</item>
    </style>

If the theme is not applied, everything works fine.

In your Context.addLifecycleObserver() extension you should make a recursive call on baseContext until it is actually a LifecycleOwner, and don't assert baseContext is a LifecycleOwner.

Best regards, Christophe.

leandroBorgesFerreira commented 5 years ago

Hello Christophe. Thanks for the tip. I'll take a look at this soon.

amannepid commented 5 years ago

I am also facing the same issue when applied theme in BottomSheetDialogFragment. When can we expect the solution ?

leandroBorgesFerreira commented 5 years ago

This is fixed in the version 2.1.1

leandroBorgesFerreira commented 5 years ago

Actually, it's not hahaha. Sorry, I mixed the versions and issues.

leandroBorgesFerreira commented 5 years ago

Try the version 2.1.2 the problem should be fixed thanks to @cdongieux