简体   繁体   English

java.lang.ClassCastException:android.graphics.drawable.BitmapDrawable无法转换为android.graphics.drawable.LayerDrawable

[英]java.lang.ClassCastException: android.graphics.drawable.BitmapDrawable cannot be cast to android.graphics.drawable.LayerDrawable

error is generated on these code : setBadgeCount(this,icon ,"0"); 在这些代码上生成了错误:setBadgeCount(this,icon,“ 0”);

here is my code: 这是我的代码:

  MenuItem itemCart = menu.findItem(R.id.action_cart);
    LayerDrawable icon = (LayerDrawable) itemCart.getIcon();

    // Update LayerDrawable's BadgeDrawable

    setBadgeCount(this,icon ,"0");

You get ClassCastException because your MenuItem returns not BitmapDrawable , you can create LayerDrawable from BitmapDrawable you get from MenuItem 您会收到ClassCastException因为MenuItem返回的不是BitmapDrawable ,您可以从MenuItem获得的BitmapDrawable创建LayerDrawable

According to this answer ( https://stackoverflow.com/a/20138871/4142087 ), or official documentation you can do it this way: 根据此答案( https://stackoverflow.com/a/20138871/4142087 )或官方文档,您可以通过以下方式进行操作:

BitmapDrawable iconBitmap = (BitmapDrawable) itemCart.getIcon();
LayerDrawable iconLayer = new LayerDrawable(new Drawable [] { iconBitmap });
setBadgeCount(this, iconLayer, "0");

Change the menu items icon drawable from vector drawable to layer-list. 将可绘制的菜单项图标从可绘制的矢量更改为图层列表。 In the layer list you can include the vector items like this. 在图层列表中,您可以包括这样的矢量项。 Here the cart icon is a vector. 这里的购物车图标是矢量。

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:drawable="@drawable/cart_icon"
        android:gravity="center" />
    <item
        android:id="@+id/ic_badge"
        android:drawable="@drawable/cart_icon" />
</layer-list>

暂无
暂无

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

相关问题 java.lang.ClassCastException:android.graphics.drawable.LayerDrawable无法强制转换为android.graphics.drawable.GradientDrawable - java.lang.ClassCastException: android.graphics.drawable.LayerDrawable cannot be cast to android.graphics.drawable.GradientDrawable 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 ClassCastException: android.support.v4.graphics.drawable.DrawableWrapperHoneycomb 不能转换为 android.graphics.drawable.LayerDrawable - ClassCastException: android.support.v4.graphics.drawable.DrawableWrapperHoneycomb cannot be cast to android.graphics.drawable.LayerDrawable android.graphics.drawable.BitmapDrawable 不能转换为 java.lang.CharSequence - android.graphics.drawable.BitmapDrawable cannot be cast to java.lang.CharSequence IonDrawable无法转换为android.graphics.drawable.BitmapDrawable - IonDrawable cannot be cast to android.graphics.drawable.BitmapDrawable NinePatchDrawable无法转换为android.graphics.drawable.BitmapDrawable - NinePatchDrawable cannot be cast to android.graphics.drawable.BitmapDrawable java.lang.ClassCastException:android.graphics.drawable.ColorDrawable无法转换为android.graphics.drawable.AnimationDrawable - java.lang.ClassCastException: android.graphics.drawable.ColorDrawable cannot be cast to android.graphics.drawable.AnimationDrawable android.support.graphics.drawable.VectorDrawableCompat 不能转换为 android.graphics.drawable.LayerDrawable - android.support.graphics.drawable.VectorDrawableCompat cannot be cast to android.graphics.drawable.LayerDrawable android.graphics.drawable.LayerDrawable 无法转换为 android.graphics.drawable.ColorDrawable - android.graphics.drawable.LayerDrawable cannot be cast to android.graphics.drawable.ColorDrawable android.graphics.drawable.AdaptiveIconDrawable 无法转换为 android.graphics.drawable.BitmapDrawable 错误 - android.graphics.drawable.AdaptiveIconDrawable cannot be cast to android.graphics.drawable.BitmapDrawable error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM