Could not find class 'android.graphics.drawable.RippleDrawable'
Asked Answered
B

2

8

I'm using a third party library, there is a method using DrawableCompat.setTintMode(drawable, PorterDuff.Mode.SRC_IN);. When I test my app in android 5.0+,there was no problem and worked well.But when it came to android 4.4.4, it threw an Exception:

    05-09 13:15:15.030 26447-26447/com.wizchen.athit E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.wizchen.athit, PID: 26447
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.wizchen.athit/com.wizchen.athit.view.activity.MainActivity}: java.lang.NullPointerException
   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2271)
   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2320)
   at android.app.ActivityThread.access$800(ActivityThread.java:138)
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1269)
   at android.os.Handler.dispatchMessage(Handler.java:102)
   at android.os.Looper.loop(Looper.java:136)
   at android.app.ActivityThread.main(ActivityThread.java:5117)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:515)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
   at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
   at android.support.v4.graphics.drawable.DrawableWrapperDonut.setCompatTintMode(DrawableWrapperDonut.java:278)
   at android.support.v4.graphics.drawable.DrawableCompatBase.setTintMode(DrawableCompatBase.java:48)
   at android.support.v4.graphics.drawable.DrawableCompat$BaseDrawableImpl.setTintMode(DrawableCompat.java:99)
   at android.support.v4.graphics.drawable.DrawableCompat.setTintMode(DrawableCompat.java:400)
   at com.wizchen.athit.lib.AppThemeEngine.util.TintHelper.createTintedDrawable(TintHelper.java:359)
   at com.wizchen.athit.lib.AppThemeEngine.viewprocessors.ToolbarProcessor.process(ToolbarProcessor.java:117)
   at com.wizchen.athit.lib.AppThemeEngine.viewprocessors.ToolbarProcessor.process(ToolbarProcessor.java:44)
   at com.wizchen.athit.lib.AppThemeEngine.ATE.postApply(ATE.java:209)
   at com.wizchen.athit.lib.AppThemeEngine.ATEActivity.onStart(ATEActivity.java:60)
   at com.wizchen.athit.view.activity.MainActivity.onStart(MainActivity.java:68)
   at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1183)
   at android.app.Activity.performStart(Activity.java:5359)
   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2244)
   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2320) 
   at android.app.ActivityThread.access$800(ActivityThread.java:138) 
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1269) 
   at android.os.Handler.dispatchMessage(Handler.java:102) 
   at android.os.Looper.loop(Looper.java:136) 
   at android.app.ActivityThread.main(ActivityThread.java:5117) 
   at java.lang.reflect.Method.invokeNative(Native Method) 
   at java.lang.reflect.Method.invoke(Method.java:515) 
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785) 
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) 
   at dalvik.system.NativeStart.main(Native Method) 

Except the stack trace above, I saw some error info printed in my console like this:

05-09 13:15:15.030 26447-26447/com.wizchen.athit E/dalvikvm: Could not find class 'android.graphics.drawable.RippleDrawable', referenced from method com.wizchen.athit.lib.AppThemeEngine.util.TintHelper.setTintAuto

And this one:

05-09 13:15:15.030 26447-26447/com.wizchen.athit E/dalvikvm: Could not find class 'android.graphics.drawable.RippleDrawable', referenced from method com.wizchen.athit.lib.AppThemeEngine.util.TintHelper.setTintSelector

Of course I have compiled the v4 support library in my project :)

Who have encountered this problem? Thanks for help!

------------------------------------------------------------------------------------------
Update 1: this is what causes NullPointerException:

    @CheckResult
    @Nullable
    public static Drawable createTintedDrawable(@Nullable Drawable drawable, @ColorInt int color) {
        if (drawable == null) return null;
        drawable = DrawableCompat.wrap(drawable.mutate());
        DrawableCompat.setTintMode(drawable, PorterDuff.Mode.SRC_IN);
        DrawableCompat.setTint(drawable, color);
        return drawable;
    }
Beanstalk answered 9/5, 2016 at 5:57 Comment(7)
post the relevant java code here.Kellen
why you think v4 support library has rippledrawablePhonogram
@wizChen see my answer below.Kellen
@Harshad I updated my question and post the relevant java code.Beanstalk
@JiangYD I updated my question and posted the relevant code. Exception appeared in DrawableCompat.setTintMode(drawable, PorterDuff.Mode.SRC_IN);. And I guess maybe the v4 support library cause this problem. And I just can't find why the error information Could not find class 'android.graphics.drawable.RippleDrawable', referenced from method com.wizchen.athit.lib.AppThemeEngine.util.TintHelper.setTintAutoappeared prior to the NullPointerException stack trace. They seem have no connection each other.Beanstalk
1) Use compileSdkVersion = 23. Use support libs version 23.x.x, newest right now is 23.3.0. 2) Check your drawables. You cannot use RippleDrawable below API 21.Octan
@EugenPechanec Yep, part of my build.config file: compileSdkVersion: 23, buildToolsVersion: "23.0.2", minSdkVersion : 16, targetSdkVersion : 23,. And my v4 support library version now is '23.3.0'Beanstalk
K
5

Read this :

If you look at the source code for DrawableCompat you will see that for any version < support library 21 the method does nothing.

The idea of DrawableCompat seems to be simply not crashing on old versions, rather than actually providing that functionality.

UseFul Tips :

With support library 22.1 you can use DrawableCompat to tint drawables.

DrawableCompat.wrap(Drawable) and setTint(), setTintList(), and setTintMode() will just work: no need to create and maintain separate drawables only to support multiple colors!

Kellen answered 9/5, 2016 at 6:10 Comment(5)
"version < 21" of what? Better say it's support library and not anything else.Octan
@EugenPechanec You can find from the code that the exception happened in DrawableCompat.setTintMode(drawable, PorterDuff.Mode.SRC_IN);, so I am confused at your answer instead.Beanstalk
@wizChen you can not DrawableCompat.setTintMode(drawable, PorterDuff.Mode.SRC_IN); below API level 21.Kellen
@Harshad OK, let me try another way!Beanstalk
@Harshad Yes you can tint drawables below API 21. If you're using support libraries v22.1.0 or higher DrawableCompat.setTint* works as expected, read section "Drawable tinting" here. Why is your answer so confusing? In the first paragraph you say it's impossible and in the second you immediately deny it.Octan
F
0

i used ?colorPrimary for creating button background and its sake error in my project , and i change ?colorPrimary to @color/colorPrimary and its work for me :)

Forrester answered 29/1, 2018 at 11:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.