【问题标题】:java.lang.NoClassDefFoundError: Failed resolution of: Landroid/graphics/BlendModeColorFilter;java.lang.NoClassDefFoundError:解析失败:Landroid/graphics/BlendModeColorFilter;
【发布时间】:2020-05-14 03:41:29
【问题描述】:

当我在模拟器上运行此应用程序时,它会崩溃。导致它崩溃的行是v.getBackground().setColorFilter

但是,在实际设备上测试应用程序时没有问题。

button.setOnTouchListener(new View.OnTouchListener() {
    public boolean onTouch(View v, MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN: {
                v.getBackground().setColorFilter(new BlendModeColorFilter(0xe0f47521, BlendMode.SRC_ATOP));
                v.invalidate();
                break;
            }
            case MotionEvent.ACTION_UP: {
                v.getBackground().clearColorFilter();
                v.invalidate();
                break;
            }
        }
        return false;
    }
});

【问题讨论】:

  • The BlendModeColorFilter class 直到 API 级别 29 才引入。尝试在以前的版本上使用它会抛出该异常。
  • @MikeM。我只是好奇,为什么在实际设备上调试时没有相同的错误?只有在使用模拟器时才会崩溃。
  • 我猜设备是API级别29或以上,但模拟器是旧版本。
  • @MikeM。你是对的。感谢您的帮助。

标签: java android noclassdeffounderror


【解决方案1】:

如前所述,直到 API 级别 29 才引入 BlendModeColorFilter。如果您问我,应该在构建时抛出错误,但我离题了...

在我看来,最好的解决方案是使用BlendModeColorFilterCompat,如here 所述,在最近的 androidx 核心版本中可用。

这会给:

v.getBackground().setColorFilter(BlendModeColorFilterCompat.createBlendModeColorFilterCompat(-0x1f0b8adf, BlendModeCompat.SRC_ATOP));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-12-25
    • 2022-08-03
    • 2017-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-04
    相关资源
    最近更新 更多