简体   繁体   English

Android - 类型ID的预期资源

[英]Android - Expected Resource of type ID

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 但是,当我调用TITLE_ID和REVIEW_ID时,它会给我一个错误

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. 例如,在调用Resources.getString(int id)时,您应该传递R.string.something,而不是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. 例如,在调用View#setLayoutDirection时,参数必须是android.view.View.LAYOUT_DIRECTION_LTR或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? 任何的想法?

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. 这只是编辑器验证错误(lint警告),因为这不是处理ID的常用方法。

So if your app supporting API 17 and higher, 因此,如果您的应用支持API 17及更高版本,

you can call View.generateViewId as 你可以调用View.generateViewId作为

  titleView.setId(View.generateViewId());

and

  sv.setId(View.generateViewId());

and for API<17 并且API <17

  1. open your project's res/values/ folder 打开项目的res/values/文件夹
  2. create an xml file called ids.xml 创建一个名为ids.xml的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'. 在Android Studio中单击灯泡上的此“错误”。 And select Disable inspection in first submenu. 然后在第一个子菜单中选择“ 禁用检查”

You can also disable lint at build.gradle file. 您还可以在build.gradle文件中禁用lint。 Add these lines in your build.gradle file. 在build.gradle文件中添加这些行。

android { 
       lintOptions{
             disable "ResourceType"
       }
}

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. 我将此作为替代“修复”那些无法生成视图ID的问题(即在视图实际存在之前定义ID)以及知道他们在做什么。

Directly above a variable declaration or method that contains the issue, simply include @SuppressWarnings("ISSUE_IDENTIFIER") to disable the lint warning for that instance. 在包含该问题的变量声明或方法的正上方,只需包含@SuppressWarnings("ISSUE_IDENTIFIER")以禁用该实例的lint警告。

In this case, it would be @SuppressWarnings("ResourceType") 在这种情况下,它将是@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: 确保撤消Disable inspection选项并从build.gradle中删除这些行:

android {
    lintOptions{
        disable "ResourceType"
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM