Я хочу сделать значок нарисованным. когда я пытался построить результаты ошибок: java .lang.ClassCastException: androidx.appcompat.widget.AppCompatImageButton не может быть приведен к android .graphics.drawable.LayerDrawable. Я не знаю, как ее решить. Кто-нибудь может помочь?
Активность
class DetailItemViewActivity : AppCompatActivity(), SwipeRefreshLayout.OnRefreshListener {
private val _tag = DetailItemViewActivity::class.java.simpleName
var notificationCountCart = 0
@SuppressLint("ResourceType")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_details)
}
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
supportActionBar?.setDisplayShowCustomEnabled(true)
supportActionBar?.setDisplayHomeAsUpEnabled(false)
supportActionBar?.setDisplayShowTitleEnabled(false)
supportActionBar?.setDisplayShowHomeEnabled(false)
var actionView = layoutInflater.inflate(R.layout.menu_app_bar,null)
supportActionBar?.setCustomView(actionView)
var actionBack = actionView.findViewById<ImageView>(R.id.btn_appbar_back)
actionBack.setOnClickListener {
val i = Intent(it.context, MainActivity::class.java)
it.context.startActivity(i)
}
var actionCart = actionView.findViewById<ImageView>(R.id.btn_appbar_cart)
actionCart.setOnClickListener{
notificationCountCart++
NotificationCountSetClass.setNotifyCount(notificationCountCart)
invalidateOptionsMenu()
val i = Intent(it.context, CartViewActivity::class.java)
it.context.startActivity(i)
}
return true
}
override fun onPrepareOptionsMenu(menu: Menu?): Boolean {
var actionView = layoutInflater.inflate(R.layout.menu_app_bar,null)
var item = actionView.findViewById<ImageButton>(R.id.btn_appbar_cart)
NotificationCountSetClass.setAddToCart(this@DetailItemViewActivity, item, notificationCountCart!!)
invalidateOptionsMenu()
return super.onPrepareOptionsMenu(menu)
}
NotificationCountSetClass
class NotificationCountSetClass : Activity() {
companion object {
private var icon: LayerDrawable? = null
fun setAddToCart(context: Context, item:ImageButton, numMessage: Int) {
icon= item.btn_appbar_cart as LayerDrawable
SetNotificationCount.setBadgeCount(context, icon!!, NotificationCountSetClass.setNotifyCount(numMessage))
}
fun setNotifyCount(numMessage: Int): Int {
return numMessage
}
}
}
объект SetNotificationCount
object SetNotificationCount {
fun setBadgeCount(context: Context, icon:LayerDrawable, count:Int) {
val badge: BadgeDrawable
val reuse = icon.findDrawableByLayerId(R.id.notice_badge)
if (reuse !=null && reuse is BadgeDrawable) {
badge = reuse
} else {
badge = BadgeDrawable(context)
}
badge.setCount(count)
icon.mutate()
icon.setDrawableByLayerId(R.id.notice_badge, badge)
}
}