Android - Expected Resource of type ID

48,407

Solution 1

This is not a compiler error. It is just editor validation error(lint warning) as this is not a common way to deal with Ids.

So if your app supporting API 17 and higher,

you can call View.generateViewId as

  titleView.setId(View.generateViewId());

and

  sv.setId(View.generateViewId());

and for API<17

  1. open your project's res/values/ folder
  2. create an xml file called ids.xml

with the following content:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <item name="titleId" type="id" />
    <item name="svId" type="id" />
</resources>

then in your code,

  titleView.setId(R.id.titleId);

and

  sv.setId(R.id.svId);

And to disable this warning (If you want)

In Android Studio click on light bulb on line with this 'error'. And select Disable inspection in first submenu.

Solution 2

You can also disable lint at build.gradle file. Add these lines in your build.gradle file.

android { 
       lintOptions{
             disable "ResourceType"
       }
}

Solution 3

I am including this as an alternative to "fixing" the issue for those that cannot generate a view ID (ie. defining the ID before the view is actually present) and know what they are doing.

Directly above a variable declaration or method that contains the issue, simply include @SuppressWarnings("ISSUE_IDENTIFIER") to disable the lint warning for that instance.

In this case, it would be @SuppressWarnings("ResourceType")

Using general methods to disable the warning type is bad practice and can lead to unforeseen issues, such as memory leaks and unstable code. Don't publish garbage.

Make sure to undo the option to Disable inspection and remove these lines from the build.gradle:

android {
    lintOptions{
        disable "ResourceType"
    }
}
Share:
48,407

Related videos on Youtube

The Newbie
Author by

The Newbie

Updated on May 05, 2020

Comments

  • The Newbie
    The Newbie about 4 years

    I have this code

    final static int TITLE_ID = 1;
    final static int REVIEW_ID = 2;
    

    Now, I want to create a new layout in my main class

    public View createContent() {
        // create linear layout for the entire view
        LinearLayout layout = new LinearLayout(this);
        layout.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT));
        layout.setOrientation(LinearLayout.VERTICAL);
    
        // create TextView for the title
        TextView titleView = new TextView(this);
        titleView.setId(TITLE_ID);
        titleView.setTextColor(Color.GRAY);
        layout.addView(titleView);
    
        StarView sv = new StarView(this);
        sv.setId(REVIEW_ID);
        layout.addView(sv);
    
        return layout;
    }
    

    But when ever I call TITLE_ID and REVIEW_ID, it gives me an error

    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.

    I don't have any problem running this code. I'm just wondering why it gives me an error. Any idea?

    • onof
      onof about 9 years
      Maybe there is a conflict. Set TITLE_ID and REVIEW_ID to very big numbers and try again.
    • Somasundaram NP
      Somasundaram NP about 9 years
      you can manage using setTag() instead of SetId
  • Mitulát báti
    Mitulát báti over 8 years
    When using generateViewId: What if I want to reference later a specific view? How would I know the id? Let's say the 10th out of 50 generated views?
  • SMBiggs
    SMBiggs about 7 years
    The light bulb->inspection 'Constant and Resource Type Mismatches options'->Disable inspection trick was just what I needed!
  • Abandoned Cart
    Abandoned Cart about 7 years
    @Erwinus it will remove the lint flag for any issue related to ResourceType. It works to take away yellow marks, but is bad practice in coding. I have posted an alternative for only removing the lint flag for the specific instance.
  • Abandoned Cart
    Abandoned Cart about 7 years
    Disabling inspection is bad practice. There are much better ways to disable the lint check on an instance without crippling lint altogether.