Reputation: 62411
I don't know why it happens:
If I write the following code:
TextView textView = new TextView(this);
textView.setId(1);
the 1 as id not working and giving me a warning like:
Its not working in following values:
textView.setId(0+1); // Valid
textView.setId(var++); // Valid even var=0
but not valid
textView.setId(1); // Invalide
Anyone know about this? Anyone can explain this things?
Upvotes: 4
Views: 826
Reputation: 1226
This warning is generated by android lint. It checks you are using the right type of ID as identifier. Signature for this function is:
public void setId(@IdRes int id)
So, lint checks you are using exactly id of type @IdRes
(see IdRes annotation), not @ColorRes or @StringRes (it checks you are using constants from class R.id
)
By using expressions like 0+1
or var++
(BTW, it's result actually is 0 not 1) you are hampering lint to infer the argument type.
Upvotes: 1
Reputation: 1085
You get a warning because setting your own ID's could be dangerous, you could for example set the same ID as an already existing xml element, this will obviously collide and cause problems. Use View.generateViewId()
instead to prevent this.
Upvotes: 0