leandroBorgesFerreira / LoadingButtonAndroid

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

cannot be cast to kotlin.jvm.functions.Function0 #182

Open mattwatson-dev opened 3 years ago

mattwatson-dev commented 3 years ago

Coding in java.

    loginButton.startAnimation();

... loginButton.revertAnimation(new OnAnimationEndListener() { //*LoginActivity.java:54 below @Override public void onAnimationEnd() { System.out.println("HIT"); } });

 Caused by: java.lang.ClassCastException: com.example.app.LoginActivity$1 cannot be cast to kotlin.jvm.functions.Function0
    at br.com.simplepass.loadingbutton.customViews.ProgressButton$DefaultImpls.revertAnimation(ProgressButton.kt:76)
    at br.com.simplepass.loadingbutton.customViews.CircularProgressButton.revertAnimation(CircularProgressButton.kt:21)
    at com.example.app.LoginActivity.onCreate(LoginActivity.java:54) //This is the revert animation line above*
    at android.app.Activity.performCreate(Activity.java:8000)
    at android.app.Activity.performCreate(Activity.java:7984)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3422)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601) 
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85) 
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066) 
    at android.os.Handler.dispatchMessage(Handler.java:106) 
    at android.os.Looper.loop(Looper.java:223) 
    at android.app.ActivityThread.main(ActivityThread.java:7656) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947) 

Really enjoying your work! Let me know if you can help.

xitake commented 9 months ago

Coding in java.

    loginButton.startAnimation();

... loginButton.revertAnimation(new OnAnimationEndListener() { //*LoginActivity.java:54 below @OverRide public void onAnimationEnd() { System.out.println("HIT"); } });

 Caused by: java.lang.ClassCastException: com.example.app.LoginActivity$1 cannot be cast to kotlin.jvm.functions.Function0
    at br.com.simplepass.loadingbutton.customViews.ProgressButton$DefaultImpls.revertAnimation(ProgressButton.kt:76)
    at br.com.simplepass.loadingbutton.customViews.CircularProgressButton.revertAnimation(CircularProgressButton.kt:21)
    at com.example.app.LoginActivity.onCreate(LoginActivity.java:54) //This is the revert animation line above*
    at android.app.Activity.performCreate(Activity.java:8000)
    at android.app.Activity.performCreate(Activity.java:7984)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3422)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601) 
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85) 
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066) 
    at android.os.Handler.dispatchMessage(Handler.java:106) 
    at android.os.Looper.loop(Looper.java:223) 
    at android.app.ActivityThread.main(ActivityThread.java:7656) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947) 

Really enjoying your work! Let me know if you can help.

Did you find out how to fix this? I'm having the exact same problem using the latest version (2.3.0)

leandroBorgesFerreira commented 9 months ago

Hi

I'll investigate the problem.

Do you have a project where the problem can be reproduced? That would make my investigation much easier.

If not, can you post more code and more context about how you are using the library?

Thanks!

xitake commented 9 months ago

Hi,

I found a workaround. As @mattwatson-dev stated, this problem happens when coding in Java instead of Kotlin.

The exception happens when passing either a lambda function or an OnAnimationEndListener instance to startAnimation:

circularProgressButton.startAnimation(() -> { Log.d("Minha Tag", "Just a log"); });

The workaround is to pass a Function0 instance instead: circularProgressButton.startAnimation(new Function0() { @Override public Object invoke() { Log.d("Minha Tag", "Just a log"); } });