Android如何引用棒棒糖RippleDrawable中较新的类

Android如何引用棒棒糖RippleDrawable中较新的类,android,android-5.0-lollipop,rippledrawable,Android,Android 5.0 Lollipop,Rippledrawable,我有一个在代码中创建RippleDrawable的方法 public class StateApplier { @TargetApi(Build.VERSION_CODES.LOLLIPOP) private static void add_Ripple(Resources res, StateListDrawable states , int color, int pressedColor){ Drawa

我有一个在代码中创建RippleDrawable的方法

    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);

        }
当我在棒棒糖上运行它时,它工作正常,但当我在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\u CODES.LOLLIPOP)会迫使代码在低于棒棒糖的设备上被跳过。奇怪的是,在没有调用add_Ripple()方法但调用StateApplier类中的另一个方法的活动上崩溃

注意,在调用该方法之前,我还使用了api检查

if( Build.VERSION.SDK_INT >=  Build.VERSION_CODES.LOLLIPOP){
add_Ripple(res, states, color, pressedColor);

在较新的API中引用类而不会在较旧的设备上崩溃的合适方法是什么。

我所做的是创建另一个名为StateApplierLollipop的类并移动它 处理RippleDrawable的所有代码,StateApplier中的代码将只调用StateApplierLollipop+设备中的代码。这阻止了KitKat上的车祸

public class StateApplierLollipop {  
public static void add_Ripple(Resources res, StateListDrawable states
                , int color, int pressedColor){
    ...........
   }
}

public class StateApplier{
public static void add_Ripple(Resources res, StateListDrawable states
                    , int color, int pressedColor){
if( Build.VERSION.SDK_INT >=  Build.VERSION_CODES.LOLLIPOP){
   StateApplierLollipop.add_Ripple(....

}

“我认为使用@TargetApi(Build.VERSION\u CODES.LOLLIPOP)会迫使代码在低于棒棒糖的设备上被跳过。”不,你必须实际添加代码来跳过它,例如,
如果(Build.VERSION.SDK\u INT>=21){…}
这是关于是否运行代码的条件语句,并且我已经在使用它。但是@TargetApi(Build.VERSION…应该通过让编译器在编译时跳过这些代码段来防止编译错误。不,这根本不是注释所做的。它只是告诉linter它不应该警告注释方法中的API级别。检查javadocs。你找到解决方案了吗?我也有同样的问题。我我已经发布了一个解决方案,我在下面用它作为答案。