【问题标题】:Android how do you reference a newer Class from the Lollipop RippleDrawableAndroid 如何从 Lollipop RippleDrawable 中引用一个较新的类
【发布时间】:2015-03-12 10:56:13
【问题描述】:

我有一种在代码中创建 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);

        }

当我在 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) 会强制在低于棒棒糖的设备上跳过代码。奇怪的是,甚至不调用方法 add_Ripple() 而是调用 StateApplier 类中的另一个方法的活动崩溃。

请注意,在调用方法之前我也在使用 api 检查

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

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

【问题讨论】:

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

标签: android android-5.0-lollipop rippledrawable


【解决方案1】:

我所做的是创建另一个名为 StateApplierLollipop 的类并移动 所有处理 RippleDrawable 的代码,StateApplier 中的代码只会为 Lollipop+ 设备调用 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(....

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-08-27
    • 2015-03-03
    • 1970-01-01
    • 1970-01-01
    • 2015-06-14
    • 2015-01-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多