Issue
I have a custom view called IconView
, which has the following constructor for initialization:
public class IconView extends RelativeLayout implements Checkable {
...
public IconView(Context context, AttributeSet attrs, boolean useDefaultImage) {
super(context, attrs);
inflateLayout(context);
...
In order to initialize AttributeSet
instance from XMLfor constructing the view, I use getResources().getXml(R.layout.icon_view), false);
.
This compiles successfully and runs correctly. However, Android studio highlights the code and displays me this error:
The detailed description of the error is here:
Expected resource of type xml less... (Ctrl+F1) Reports two types of problems:
Supplying the wrong type of resource identifier. For example, when calling Resources.getString(int id), you should be passing R.string.something, not R.drawable.something.
Passing the wrong constant to a method which expects one of a specific set of constants. For example, when calling View#setLayoutDirection, the parameter must be android.view.View.LAYOUT_DIRECTION_LTR or android.view.View.LAYOUT_DIRECTION_RTL.
The question:
Although the code works, I do not know, how to rewrite it, so that the error would disappear in Android Studio. The error is visually annoying, so how could I get rid of it?
Solution
Resources#getXml(int id)
is used to get the resource of type xml, which lays inside the xml
resource folder. You, on the other hand, passing R.layout.icon_view
here, the layout resource.
Use getResources().getLayout(R.layout.icon_view)
here and the error will disappear.
P.S.: the documentation on Resources#getLayout()
says the following:
This function is really a simple wrapper for calling getXml(int) with a layout resource.
So it looks like this is some kind of lint-related issue. Either way, getLayout()
does not result in this error.
Answered By - aga
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.