您好,欢迎来到二三四教育网。
搜索
您的当前位置:首页解决动态创建view并且setId的报错的问题

解决动态创建view并且setId的报错的问题

来源:二三四教育网

有时候我们会动态创建view,并且需要给其赋予一个id,我们可能会这样写:

llBody=newLinearLayout(getContext());

llBody.setId(1234);

这样写运行并不会报错,但是编译器会提示:

Expected resource of type id less... (Ctrl+F1)

This inspection looks at Android API calls that have been annotated with various support annotations (such as RequiresPermission or UiThread) and flags any calls that are not using the API correctly as specified by the annotations. Examples of errors flagged by this inspection:

Passing the wrong type of resource integer (such as R.string) to an API that expects a different type (such as R.dimen).

Forgetting to invoke the overridden method (via super) in methods that require it

Calling a method that requires a permission without having declared that permission in the manifest

Passing a resource color reference to a method which expects an RGB integer value.

...and many more.

并且打签名包会提示error,但是不会影响打包的成功。

作为一个程序员坚决不能容忍小红线,可以这么修改:

1、API 17+

setId(View.generateViewId())

2、API小于17时:

llBody.setId(newInteger(2));

以上,小红线没了!打签名包不会提示error。

Copyright © 2019- how234.cn 版权所有 赣ICP备2023008801号-2

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务