Skip to content

ClassCastException for Drawable in CircularProgressButton #25

@msrd0

Description

@msrd0

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions