本文介绍了java.lang.ClassCastException:android.graphics.drawable.BitmapDrawable无法转换为android.graphics.drawable.LayerDrawable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在这些代码上生成错误:setBadgeCount(this,icon,"0");
这是我的代码:
MenuItem itemCart = menu.findItem(R.id.action_cart);
LayerDrawable icon = (LayerDrawable) itemCart.getIcon();
// Update LayerDrawable's BadgeDrawable
setBadgeCount(this,icon ,"0");
解决方案
您会得到ClassCastException
,因为您的MenuItem
返回的不是BitmapDrawable
,因此可以从MenuItem
返回的BitmapDrawable
中创建LayerDrawable
/p>
根据此答案( https://stackoverflow.com/a/20138871/4142087 )或官方文档,您可以这样操作:
BitmapDrawable iconBitmap = (BitmapDrawable) itemCart.getIcon();
LayerDrawable iconLayer = new LayerDrawable(new Drawable [] { iconBitmap });
setBadgeCount(this, iconLayer, "0");
error is generated on these code :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
According to this answer (https://stackoverflow.com/a/20138871/4142087), or official documentation you can do it this way:
BitmapDrawable iconBitmap = (BitmapDrawable) itemCart.getIcon();
LayerDrawable iconLayer = new LayerDrawable(new Drawable [] { iconBitmap });
setBadgeCount(this, iconLayer, "0");
这篇关于java.lang.ClassCastException:android.graphics.drawable.BitmapDrawable无法转换为android.graphics.drawable.LayerDrawable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!