我有一个在代码中创建涟漪可绘制对象的方法。
public class StateApplier {
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private static void add_Ripple(Resources res, StateListDrawable states
, int color, int pressedColor){
Drawable rd = new android.graphics.drawable.RippleDrawable(get_Ripple_ColorSelector(pressedColor)
, new ColorDrawable(color), null);
states.addState(new int[] {}, rd);
}
这段代码在Lollipop系统上运行良好,但是在KitKat系统上则崩溃了。以下是错误日志:
03-12 21:36:47.734: E/dalvikvm(26295): Could not find class 'android.graphics.drawable.RippleDrawable', referenced from method com.acme.applib.Render.StateApplier.add_Ripple
03-12 21:36:47.734: W/dalvikvm(26295): VFY: unable to resolve new-instance 149 (Landroid/graphics/drawable/RippleDrawable;) in Lcom/acme/applib/Render/StateApplier;
03-12 21:36:47.734: D/dalvikvm(26295): VFY: replacing opcode 0x22 at 0x0000
03-12 21:36:47.738: W/dalvikvm(26295): VFY: unable to find class referenced in signature (Landroid/graphics/drawable/RippleDrawable;)
03-12 21:36:47.738: W/dalvikvm(26295): VFY: returning Ljava/lang/Object; (cl=0x0), declared Landroid/graphics/drawable/Drawable; (cl=0x0)
03-12 21:36:47.738: W/dalvikvm(26295): VFY: rejecting opcode 0x11 at 0x0004
03-12 21:36:47.738: W/dalvikvm(26295): VFY: rejected Lcom/acme/applib/Render/StateApplier;.create_Ripple (Landroid/content/res/Resources;II)Landroid/graphics/drawable/Drawable;
03-12 21:36:47.738: W/dalvikvm(26295): Verifier rejected class Lcom/acme/applib/Render/StateApplier;
我认为使用 @TargetApi(Build.VERSION_CODES.LOLLIPOP) 应该能够让代码在低于Lollipop版本的设备上跳过。但是奇怪的是,在一个根本没有调用add_Ripple()方法但调用StateApplier类中另一个方法的活动中,它仍然会崩溃。
请注意,在调用该方法之前我还使用了API检查。
if( Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
add_Ripple(res, states, color, pressedColor);
在较老的设备上不会崩溃的情况下,引用新版API中的类的适当方法是什么。
if (Build.VERSION.SDK_INT >= 21) { ... }
。 - alanv