0

I am passing my custom object sub which has a color property from an activity to another and retrieving it like this:

val intent = this.intent
val bundle = intent.extras
sub = bundle.getParcelable("selected")

then, when a button is pressed a color picker shows up and lets me select a color, I have this method that listens for the color selection:

override fun onColorSelected(dialogId: Int, color: Int) {
    sub.color = color
    createsub_rel.backgroundColor = color
}

as you can see the color is returned as an Int.

The Exceptions happens in the onBindViewHolder() of my RecyclerView, specifically on this line:

viewHolder.relativeLayout.setBackgroundColor(mContext.getColor(sub.color))

the log states:

android.content.res.Resources$NotFoundException: Resource ID #0x7fff9800

I have debugged it and sub.color is actually the expected value, I have looked here on SO for a solution, specifically on this, but I couldn't find any working answer.

Daniele
  • 4,163
  • 7
  • 44
  • 95
  • Why do you resolve colorResourceId when you already have the color value? Is `...setBackgroundColor(sub.color)` not working? – Pawel May 01 '18 at 20:29
  • @Pawel yes, it's happening on `viewHolder.relativeLayout.setBackgroundColor(mContext.getColor(sub.color)`, didn't quite understand your first question though, I am getting the value to display it in the recyclerView – Daniele May 01 '18 at 20:33
  • 1
    `Context.getColor(int)` is used to resolve color from resources like `R.color.colorAccent`, you should not pass ARGB value as its argument. – Pawel May 01 '18 at 20:37
  • @Pawel yes, I think that's it, I need to figure out a way to distinguish between the two cases, cause normally I get the colors from resources, not when I change it though, do I need to create a separate adapter, or how can I tell apart the two cases – Daniele May 01 '18 at 20:40
  • If you can modify structure of `sub`, you can add a field indicating color format and add an `if(...)` block into your bind method, or wrap the color into custom object that has `getColor(context)` method. – Pawel May 01 '18 at 20:48
  • @Pawel This makes sense, I will do this way, thanks for the help! – Daniele May 01 '18 at 20:53

0 Answers0