Timo
Timo

Reputation: 5390

Initializing custom view in Android Studio: Expected resource of type xml

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:

Screenshot of the error in Android Studio

The detailed description of the error is here:

Expected resource of type xml less... (Ctrl+F1) Reports two types of problems:

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?

Upvotes: 1

Views: 3894

Answers (1)

aga
aga

Reputation: 29416

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.

Upvotes: 4

Related Questions