简体   繁体   English

无法在Android中将Drawable转换为LayerDrawable

[英]Unable to cast Drawable to LayerDrawable in android

I want to display notification badge on menu item. 我想在菜单项上显示通知徽章。 My code is 我的代码是

onPrepareOptionsMenu() onPrepareOptionsMenu()

    int cartSize = MyCart.getCartSize();
    MenuItem cartItem = menu.findItem(R.id.toolbar_ic_cart);

    if (cartSize > 0) {
        LayerDrawable icon = (LayerDrawable) cartItem.getIcon();
        NotificationBadgeCount.setBadgeCount(this, icon, String.valueOf(cartSize));
    } else {
        cartItem.setIcon(R.drawable.ic_cart_empty);
    }

menu/toolbar_menu.xml 菜单/toolbar_menu.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

    <item
        android:id="@+id/toolbar_ic_search"
        android:title="Search"
        android:icon="@drawable/ic_search"
        app:showAsAction="always" />

    <item
        android:id="@+id/toolbar_ic_cart"
        android:title="Cart"
        android:icon="@drawable/ic_cart_menu"
        app:showAsAction="always" />

</menu>

@drawable/ic_cart_menu @ drawable / ic_cart_menu

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:drawable="@drawable/ic_cart_full"
        android:gravity="center" />

    <!-- set a place holder Drawable so android:drawable isn't null -->
    <item
        android:id="@+id/ic_badge"
        android:drawable="@drawable/ic_cart_empty" />

</layer-list>

This code was working fine for me. 这段代码对我来说很好用。 But after upgrading support libraries, I am getting error on all devices except Marshmallow 但是升级支持库后,除了棉花糖以外,所有设备上都出现错误

Error in following line 下一行错误

LayerDrawable icon = (LayerDrawable) cartItem.getIcon();

Error on Lollipop. 棒棒糖上的错误。

java.lang.ClassCastException: android.support.v4.graphics.drawable.DrawableWrapperLollipop cannot be cast to android.graphics.drawable.LayerDrawable

Error on Kitkat. Kitkat发生错误。

java.lang.ClassCastException: android.support.v4.graphics.drawable.DrawableWrapperKitkat cannot be cast to android.graphics.drawable.LayerDrawable

It is because DrawableWrapperLollipop extends DrawableWrapperKitkat and DrawableWrapperKitkat to some other older vesrion and so on.. But the parent class is Drawable and not LayerDrawable . 这是因为DrawableWrapperLollipop延伸DrawableWrapperKitkatDrawableWrapperKitkat一些其他老vesrion等..但是父类是Drawable ,而不是LayerDrawable That is the reason for exception. 这就是例外的原因。

Those wrapper classes all implement DrawableWrapper , from which you can get the original drawable: 这些包装器类都实现了DrawableWrapper ,从中可以获取原始的drawable:

import android.support.v4.graphics.drawable.DrawableWrapper;

... ...

    LayerDrawable icon = null;
    Drawable drawable = cartItem.getIcon();
    if (drawable instanceof DrawableWrapper) {
        drawable = ((DrawableWrapper)drawable).getWrappedDrawable();
    }
    icon = (LayerDrawable) drawable;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 android.support.graphics.drawable.VectorDrawableCompat 不能转换为 android.graphics.drawable.LayerDrawable - android.support.graphics.drawable.VectorDrawableCompat cannot be cast to android.graphics.drawable.LayerDrawable Android:将Drawable添加到现有的LayerDrawable - Android: Adding a Drawable to an existing LayerDrawable SeekBar-NinePatchDrawable无法强制转换为LayerDrawable - SeekBar - NinePatchDrawable cannot be cast to LayerDrawable 无法转换为android.graphics.drawable.ColorDrawable - Cannot be cast to android.graphics.drawable.ColorDrawable 无法使用android-gif-drawable库 - Unable to work with android-gif-drawable library Android:无法更改Fragment中按钮的背景(可绘制) - Android: Unable to change background (drawable) of a button in Fragment android.graphics.drawable.NinePatchDrawable无法转换为android.graphics.drawable.TransitionDrawable” - android.graphics.drawable.NinePatchDrawable cannot be cast to android.graphics.drawable.TransitionDrawable" java.lang.ClassCastException:android.graphics.drawable.ColorDrawable无法转换为android.graphics.drawable.BitmapDrawable - java.lang.ClassCastException: android.graphics.drawable.ColorDrawable cannot be cast to android.graphics.drawable.BitmapDrawable Android中的Animate Drawable图标-ClassCastException VectorDrawable无法转换为Animatable - Animate Drawable icon in Android - ClassCastException VectorDrawable cannot be cast to Animatable Android 将字符串名称转换为 R.drawable.name - Android Cast String name to R.drawable.name
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM