Java Android - 类型 ID 的预期资源

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29696879/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 08:27:22  来源:igfitidea点击:

Android - Expected Resource of type ID

javaandroidarcgisesri

提问by The Newbie

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.
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.

提供错误类型的资源标识符。
例如,在调用 Resources.getString(int id) 时,您应该传递 R.string.something,而不是 R.drawable.something。
将错误的常量传递给需要一组特定常量之一的方法。例如调用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?

运行此代码没有任何问题。我只是想知道为什么它会给我一个错误。任何的想法?

采纳答案by Giru Bhai

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.generateViewIdas

您可以将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
  2. create an xml file called ids.xml
  1. 打开你的项目res/values/文件夹
  2. 创建一个名为的 xml 文件 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 inspectionin first submenu.

在 Android Studio 中,点击带有此“错误”的灯泡。并在第一个子菜单中选择禁用检查

回答by Alvi

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

您还可以在 build.gradle 文件中禁用 lint。在 build.gradle 文件中添加这些行。

android { 
       lintOptions{
             disable "ResourceType"
       }
}

回答by Abandoned Cart

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 practiceand can lead to unforeseen issues, such as memory leaks and unstable code. Don't publish garbage.

使用通用方法禁用警告类型是不好的做法,可能会导致不可预见的问题,例如内存泄漏和代码不稳定。不要发布垃圾。

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

确保撤消选项Disable inspection并从 build.gradle 中删除这些行:

android {
    lintOptions{
        disable "ResourceType"
    }
}