在我的应用我用一个渐变背景。我用我的手机来调试它的棒棒糖和一切工作。今天我的手机(LG G4)更新到棉花糖,现在当梯度是假设可以得出以下堆栈跟踪应用程序崩溃:
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>
这是什么原因,如何解决?
一个可能的答案下旬,但可能有助于未来开发。对于这些类型的梯度文件,总是力求用相同的颜色格式。例如,如果您使用的是十六进制值,请在所有
<gradient
android:startColor="#3F51B5"
android:centerColor="#4CAF50"
android:endColor="#3F51B5"/>
和你引用的主题颜色,
<gradient
android:startColor="?attr/colorPrimary"
android:centerColor="?attr/colorAccent"
android:endColor="?attr/colorPrimary"/>