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
- open your project's
res/values/
folder
- 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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…