Cannot render MaterialButton with android.material

2020-02-08 14:18发布

问题:

Whenever I use MaterialButton, I get the following exception in the xml preview:

java.lang.IllegalArgumentException: java.lang.ClassCastException

I have upgraded to android studio 3.4 and to com.google.android.material:material:1.1.0-alpha05

Version 1.0.0 works but any 1.1.x doesn't.

Is this a problem with the IDE or the library?

-

For reference, the full stacktrace:

java.lang.ClassCastException@d8dc5d1
    at sun.reflect.GeneratedMethodAccessor893.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at android.animation.PropertyValuesHolder_Delegate.callMethod(PropertyValuesHolder_Delegate.java:108)
    at android.animation.PropertyValuesHolder_Delegate.nCallFloatMethod(PropertyValuesHolder_Delegate.java:143)
    at android.animation.PropertyValuesHolder.nCallFloatMethod(PropertyValuesHolder.java)
    at android.animation.PropertyValuesHolder.access$400(PropertyValuesHolder.java:38)
    at android.animation.PropertyValuesHolder$FloatPropertyValuesHolder.setAnimatedValue(PropertyValuesHolder.java:1387)
    at android.animation.ObjectAnimator.animateValue(ObjectAnimator.java:990)
    at android.animation.ValueAnimator.animateBasedOnTime(ValueAnimator.java:1339)
    at android.animation.ValueAnimator.doAnimationFrame(ValueAnimator.java:1471)
    at android.animation.ValueAnimator.pulseAnimationFrame(ValueAnimator.java:1490)
    at android.animation.AnimatorSet.pulseFrame(AnimatorSet.java:1163)
    at android.animation.AnimatorSet.handleAnimationEvents(AnimatorSet.java:1146)
    at android.animation.AnimatorSet.doAnimationFrame(AnimatorSet.java:1046)
    at android.animation.AnimationHandler.doAnimationFrame(AnimationHandler.java:146)
    at android.animation.AnimationHandler.access$100(AnimationHandler.java:37)
    at android.animation.AnimationHandler$1.doFrame(AnimationHandler.java:54)
    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:947)
    at android.view.Choreographer.doCallbacks(Choreographer.java:761)
    at android.view.Choreographer_Delegate.doFrame(Choreographer_Delegate.java:66)
    at com.android.layoutlib.bridge.impl.RenderSessionImpl.renderAndBuildResult(RenderSessionImpl.java:563)
    at com.android.layoutlib.bridge.impl.RenderSessionImpl.render(RenderSessionImpl.java:425)
    at com.android.layoutlib.bridge.BridgeRenderSession.render(BridgeRenderSession.java:120)
    at com.android.ide.common.rendering.api.RenderSession.render(RenderSession.java:151)
    at com.android.ide.common.rendering.api.RenderSession.render(RenderSession.java:133)
    at com.android.tools.idea.rendering.RenderTask.lambda$null$8(RenderTask.java:755)
    at java.util.concurrent.CompletableFuture$AsyncSupply.run(CompletableFuture.java:1590)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)

EDIT This happens only when the button style is not set OR the defaulted:

@style/Widget.MaterialComponents.Button

Setting as unelevated style works for example, so as a work around I'm using the tools namespace to show unelevated button style.

Edit 2 the app theme parent is already correctly set as Theme.AppCompat.Light.DarkActionBar. This is an IDE issue as it is running well on my emulator and device. I have also done a clear cache & invalidate, clean build, rebuild... (the standard things we do when faced with weird IDE problems)

Edit 3 Still no luck with the latest A.S. 3.4.1!

回答1:

Update3: This is finally fixed in Studio 3.5beta2! We did it guys!