8
其他人在android 4.2的StateListDrawable
中使用新的ShapeDrawable()
會出現問題?我曾經這樣做:在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());
現在這個很好用,但我想要知道爲什麼不使用默認的構造函數工作 =)
感謝您的時間:)