我有一个在代码中创建RippleDrawables的方法
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_CODES.LOLLIPOP)会迫使代码在低于棒棒糖的设备上跳过。奇怪的是,它破坏的活动甚至不调用add_Ripple()方法,而是调用StateApplier类中的另一个方法。
注意,在调用该方法之前,我还使用了api检查。
if( Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
add_Ripple(res, states, color, pressedColor);在较新的API中引用类的适当方式是什么,而不会在旧设备上崩溃。
发布于 2015-07-18 03:56:38
我所做的是创建另一个名为StateApplierLollipop的类,并将处理RippleDrawable的所有代码移到那里,而StateApplier中的代码将只在StateApplierLollipop中调用用于Lollipop+设备的代码。这阻止了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(....}
https://stackoverflow.com/questions/29007870
复制相似问题