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;
    }
wizChen
  • 788
  • 2
  • 7
  • 17
  • post the relevant java code here. –  May 09 '16 at 06:03
  • 1
    why you think `v4 support library` has `rippledrawable` – Jiang YD May 09 '16 at 06:04
  • @wizChen see my answer below. –  May 09 '16 at 06:15
  • @Harshad I updated my question and post the relevant java code. – wizChen May 09 '16 at 09:10
  • @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.setTintAuto`appeared prior to the NullPointerException stack trace. They seem have no connection each other. – wizChen May 09 '16 at 09:17
  • 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. – Eugen Pechanec May 09 '16 at 09:31
  • @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' – wizChen May 09 '16 at 09:35

2 Answers2

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!

Community
  • 1
  • 1
  • "version < 21" of what? Better say it's support library and not anything else. – Eugen Pechanec May 09 '16 at 09:27
  • @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. – wizChen May 09 '16 at 12:51
  • @wizChen you can not `DrawableCompat.setTintMode(drawable, PorterDuff.Mode.SRC_IN);` below API level 21. –  May 09 '16 at 12:53
  • @Harshad OK, let me try another way! – wizChen May 09 '16 at 12:55
  • @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](https://chris.banes.me/2015/04/22/support-libraries-v22-1-0/). Why is your answer so confusing? In the first paragraph you say it's impossible and in the second you immediately deny it. – Eugen Pechanec May 09 '16 at 14:00
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 :)