简体   繁体   中英

java.lang.NoClassDefFoundError: Failed resolution of: Landroid/graphics/BlendModeColorFilter;

This app crashes when I am running it on the emulator. The line that causes it to crash is v.getBackground().setColorFilter .

But, there is no problem when testing the app on an actual device.

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

As mentionned previously, BlendModeColorFilter wasn't introduced until API level 29. An error should be thrown at build time if you ask me but I digress...

The best solution in my opinion is to go with BlendModeColorFilterCompat available in recent androidx core releases as described here .

Which would give:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM