在我的应用程序中,我使用渐变背景。我曾经在Lollipop手机上调试它,一切正常。今天我将我的手机(LG G4)更新为Marshmallow,现在当想要绘制渐变时,应用程序崩溃并显示以下堆栈跟踪:
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
我注意到崩溃只发生在引用AppTheme值的渐变属性中,如下所示:
<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>
具有固定值的渐变效果很好:
<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>
可能导致此问题的原因以及如何解决?
答案 0 :(得分:0)
可能的答案很晚,但可能会帮助将来的开发人员。 对于那些类型的渐变文件,请始终努力使用相同的颜色格式。 例如,如果您使用十六进制值,请全部使用
<gradient
android:startColor="#3F51B5"
android:centerColor="#4CAF50"
android:endColor="#3F51B5"/>
你们中的人正在引用主题色,
<gradient
android:startColor="?attr/colorPrimary"
android:centerColor="?attr/colorAccent"
android:endColor="?attr/colorPrimary"/>