Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradient crashes with IllegalArgumentException on Android M

In my app I use a gradient background. I used to debug it with my phone on Lollipop and everything worked. Today I updated my phone (LG G4) to Marshmallow and now when the gradient is suppose to be drawn the app crashes with the following stack trace:

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.max.lucas, PID: 28163
                  java.lang.IllegalArgumentException: color and position arrays must be of equal length
                      at android.graphics.LinearGradient.<init>(LinearGradient.java:58)
                      at android.graphics.drawable.GradientDrawable.ensureValidRect(GradientDrawable.java:942)
                      at android.graphics.drawable.GradientDrawable.draw(GradientDrawable.java:516)
                      at android.view.View.getDrawableRenderNode(View.java:16451)
                      at android.view.View.drawBackground(View.java:16387)
                      at android.view.View.draw(View.java:16197)
                      at android.view.View.updateDisplayListIfDirty(View.java:15202)
                      at android.view.View.draw(View.java:15976)
                      at android.view.ViewGroup.drawChild(ViewGroup.java:3611)
                      at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3401)
                      at android.view.View.updateDisplayListIfDirty(View.java:15197)
                      at android.view.View.draw(View.java:15976)
                      at android.view.ViewGroup.drawChild(ViewGroup.java:3611)
                      at android.support.v7.widget.RecyclerView.drawChild(RecyclerView.java:3588)
                      at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3401)
                      at android.view.View.draw(View.java:16209)
                      at android.support.v7.widget.RecyclerView.draw(RecyclerView.java:3097)
                      at android.view.View.updateDisplayListIfDirty(View.java:15202)
                      at android.view.View.draw(View.java:15976)
                      at android.view.ViewGroup.drawChild(ViewGroup.java:3611)
                      at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3401)
                      at android.view.View.draw(View.java:16209)
                      at android.support.design.internal.ScrimInsetsFrameLayout.draw(ScrimInsetsFrameLayout.java:82)
                      at android.view.View.updateDisplayListIfDirty(View.java:15202)
                      at android.view.View.draw(View.java:15976)
                      at android.view.ViewGroup.drawChild(ViewGroup.java:3611)
                      at android.support.v4.widget.DrawerLayout.drawChild(DrawerLayout.java:1229)
                      at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3401)
                      at android.view.View.draw(View.java:16209)
                      at android.view.View.updateDisplayListIfDirty(View.java:15202)
                      at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3595)
                      at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3575)
                      at android.view.View.updateDisplayListIfDirty(View.java:15162)
                      at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3595)
                      at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3575)
                      at android.view.View.updateDisplayListIfDirty(View.java:15162)
                      at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3595)
                      at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3575)
                      at android.view.View.updateDisplayListIfDirty(View.java:15162)
                      at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3595)
                      at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3575)
                      at android.view.View.updateDisplayListIfDirty(View.java:15162)
                      at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3595)
                      at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3575)
                      at android.view.View.updateDisplayListIfDirty(View.java:15162)
                      at android.view.ThreadedRenderer.updateViewTreeDisplayList(ThreadedRenderer.java:281)
                      at android.view.ThreadedRenderer.updateRootDisplayList(ThreadedRenderer.java:287)
                      at android.view.ThreadedRenderer.draw(ThreadedRenderer.java:322)
                      at android.view.ViewRootImpl.draw(ViewRootImpl.java:2655)
                      at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2469)
                      at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2098)
                      at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1125)
                      at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6100)
                      at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
                      at android.view.Choreographer.doCallbacks(Choreographer.java:670)
                      at android.view.Choreographer.doFrame(Choreographer.java:606)
                      at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
                      at android.os.Handler.handleCallback(Handler.java:739)
                      at android.os.Handler.dispatchMessage(Handler.java:95)
                      at android.os.Looper.loop(Looper.java:148)
                  at android.app.ActivityThread.main

I noticed the crash occurs only with the gradient's attributes referencing the AppTheme values like so:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >
    <gradient
        android:startColor="?attr/colorPrimary"
        android:centerColor="#000066"
        android:endColor="?attr/colorPrimary"
        android:type="linear"
        android:angle="135"/>
</shape>

A gradient with fixed values works just fine:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >
    <gradient
        android:startColor="#3F51B5"
        android:centerColor="#4CAF50"
        android:endColor="#3F51B5"
        android:type="linear"
        android:angle="135"/>
</shape>

What could cause this and how do I fix it?

like image 239
iMax531 Avatar asked Feb 12 '16 13:02

iMax531


1 Answers

A possible late answer but might help future developers. For those type of gradient files, always strive to use the same color format. For example, if you are using Hex values, use in all

<gradient
    android:startColor="#3F51B5"
    android:centerColor="#4CAF50"
    android:endColor="#3F51B5"/>

and of you are referencing theme colours,

<gradient
    android:startColor="?attr/colorPrimary"
    android:centerColor="?attr/colorAccent"
    android:endColor="?attr/colorPrimary"/>
like image 110
Makamu Evans Avatar answered Sep 20 '22 05:09

Makamu Evans