8

我正在使用第三方库,有一种方法使用DrawableCompat.setTintMode(drawable, PorterDuff.Mode.SRC_IN);. 当我在 android 5.0+ 中测试我的应用程序时,没有问题并且运行良好。但是当涉及到 android 4.4.4 时,它抛出了一个异常:

    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) 

除了上面的堆栈跟踪,我在控制台中看到了一些错误信息,如下所示:

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

和这个:

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

当然我已经在我的项目中编译了 v4 支持库:)

谁遇到过这个问题?感谢帮助!

-------------------------------------------------- ----------------------------------------
更新 1: 这是导致 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;
    }
4

2 回答 2

5

读这个 :

如果您查看 DrawableCompat 的源代码,您会发现对于任何版本 <support library 21 该方法什么都不做

DrawableCompat 的想法似乎根本不会在旧版本上崩溃,而不是实际提供该功能。

有用的提示:

您可以使用support library 22.1DrawableCompat 为可绘制对象着色。

DrawableCompat.wrap(Drawable) 和 setTint()、setTintList() 和 setTintMode() 将正常工作:无需创建和维护单独的可绘制对象,仅支持多种颜色!

于 2016-05-09T06:10:19.380 回答
0

我使用 ?colorPrimary 在我的项目中创建按钮背景及其缘故错误,我将 ?colorPrimary 更改为 @color/colorPrimary 及其对我的工作:)

于 2018-01-29T11:56:57.870 回答