其他人在使用android 4.2中的新功能ShapeDrawable()
时遇到问题?StateListDrawable
我曾经这样做:
ShapeDrawable bg = new ShapeDrawable(); //default Ctor
ShapeDrawable hl = new ShapeDrawable();
hl.getPaint().setColor(color1);
bg.getPaint().setColor(color2);
StateListDrawable s1 = new StateListDrawable();
s1.addState(new int[]{android.R.attr.state_pressed}, hl);
s1.addState(new int[]{}, bg);
但这在 Android 4.2中不再起作用,抛出nullpointerexception
:
java.lang.NullPointerException
at android.graphics.drawable.ShapeDrawable.mutate(ShapeDrawable.java:387)
at android.graphics.drawable.DrawableContainer.selectDrawable(DrawableContainer.java:315)
at android.graphics.drawable.StateListDrawable.onStateChange(StateListDrawable.java:106)
at android.graphics.drawable.StateListDrawable.addState(StateListDrawable.java:89)
我通过更改我的构造函数解决了这个问题ShapeDrawable
:
ShapeDrawable bg = new ShapeDrawable(new RectShape());
ShapeDrawable hl = new ShapeDrawable(new RectShape());
现在这很好用,但我想知道为什么这不适用于默认构造函数=)
感谢您的时间 :)