Ben Pearson Ben Pearson - 4 months ago 36
Java Question

obtainStyledAttributes annotated with @StyleableRes. Suppress warnings

While looking at the answer here, I was having problems on the line:

TypedArray ta = obtainStyledAttributes(R.style.MyCustomStyle, attrs);


It seems that Android Studio won't let me pass in an int that doesn't come from a
R.styleable
resource without gettings warnings.

It tells me that it expects a resource of type styleable, which I assume means that the code I'm calling has been annotated with the @StyleableRes annotation

Expected resource of type styleable

What would be the best course of action to read the values defined in the R.style.{x}? The accepted answer on the linked post works and compiles, but I don't know how to suppress the warning. As there's a warning, is it safe to suppress? If so, how?

Answer

I think the correct way to suppress this :

@SuppressWarnings("ResourceType")

I'm offered this when I press Alt-Enter to bring up fixes for the warning and choose Inspection 'Constant and Resource Type Mismatches' options > Suppress for class (screenshot attached)enter image description here.

It works for me.

It's possible that the ID for this inspection has changed since whatever code you were referencing was written; I looked briefly through the commit logs but couldn't find a reference to it.

Comments