其他人在
android 4.2中的StateListDrawable中使用新的ShapeDrawable()时遇到问题?我曾经这样做过:
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());
现在这很好用,但我想知道为什么这对默认构造函数不起作用=)
感谢您的时间 :)