leandroBorgesFerreira / LoadingButtonAndroid

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

ClassCastException for Drawable in CircularProgressButton #25

Closed msrd0 closed 7 years ago

msrd0 commented 7 years ago

Running your code on an Android 6 Device with Kotlin and Anko, I get the following error:

Caused by: java.lang.ClassCastException: android.graphics.drawable.StateListDrawable cannot be cast to android.graphics.drawable.GradientDrawable
    at br.com.simplepass.loading_button_lib.customViews.CircularProgressButton.init(CircularProgressButton.java:116)
    at br.com.simplepass.loading_button_lib.customViews.CircularProgressButton.<init>(CircularProgressButton.java:64)
    at com.example.anko.loadingbutton.CircularprogresbuttonKt.circularProgressButton(circularprogresbutton.kt:9)
    at com.example.app.ui.LoginActivityUi.createView(loginactivity.kt:95)
    at com.example.app.ui.LoginActivityUi.createView(loginactivity.kt:55)
    at org.jetbrains.anko.AnkoContextKt.setContentView(AnkoContext.kt:138)
    at com.example.app.ui.LoginActivity.onCreate(loginactivity.kt:34)
    at android.app.Activity.performCreate(Activity.java:6372)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1110)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2432)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2539) 
    at android.app.ActivityThread.access$900(ActivityThread.java:168) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1378) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:150) 
    at android.app.ActivityThread.main(ActivityThread.java:5665) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:799) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:689) 

I tried delivering StateListDrawable.getCurrent() as background resource, but still no luck:

backgroundDrawable = (context.drawable(R.drawable.button_shape_default) as StateListDrawable).current

This is my code for integration with anko:

fun ViewManager.circularProgressButton(theme : Int = 0, init : CircularProgressButton.() -> Unit = {})
    = ankoView({ CircularProgressButton(it) }, theme, init)
fun ViewManager.circularProgressButton(text : CharSequence, theme : Int = 0, init : CircularProgressButton.() -> Unit = {})
    = circularProgressButton(theme) { init(); setText(text) }
fun ViewManager.circularProgressButton(@StringRes textRes : Int, theme : Int = 0, init : CircularProgressButton.() -> Unit = {})
    = circularProgressButton(theme) { init(); setText(textRes) }

Looking at the source code, it seems that you are only looking for the type of the Drawable if the passed AttributeSet is not null. While this is the case when inflating from xml layout, programmatically creating the button will hit that bug.

leandroBorgesFerreira commented 7 years ago

Hello @msrd0. I just fixed this issue, using a fork of this project by @jguerinet. The version 1.8.3 contains the fix.

You can check this link for a example of use with Anko DSL: https://github.com/leandroBorgesFerreira/LoadingButtonAndroid/blob/master/app/src/main/java/br/com/simplepass/loadingbutton/AnkoActivity.kt

jguerinet commented 7 years ago

@leandroBorgesFerreira I've been meaning to PR that fix back into your project, thanks for getting to it first. I've got a couple of other fixes (namely, being able to set the attributes programmatically) that I'll PR into your repo in the next couple of days.

msrd0 commented 7 years ago

@jguerinet Sounds good, for now I have copied the source code and made the mParams public, but not the best solution