Is there any legitimate use for @+android:id?

荒凉一梦 提交于 2019-12-14 03:52:39

问题


After I upgraded my android tools to latest version to work on API21, trying to compile my project resulted in the following error:

  • update_languages_button_preference_layout.xml:2: note: did you mean to use @+id instead of @+android:id?
  • update_languages_button_preference_layout.xml:2: error: creating resource for external package android: id/layout.
  • update_languages_button_preference_layout.xml:2: error: Error: No resource found that matches the given name (at 'id' with value '@+android:id/layout').

The problem seems to be the line android:id="@+android:id/layout": replacing @+android:id by @+id was enough to be able to compile and run the project normally.

However, a rapid search through the whole codebase revealed a few other locations in the project where the construction android:id="@+android:id/... is used. These apparently didn't prevent the project from passing all tests, even if I can't assure they are all still in use.

  • Is there any legitimate use of android:id="@+android:id that would justify leaving those references in our xml files, and not replacing them all by @+id?

  • Why didn't they raise the same error as the first file did?


回答1:


No, you should never use @+android:id in your app. The android namespace is reserved for the framework. This has never been safe for an app to use and will now generate a compile error.

For a little background, generating new IDs within the android namespace will put them in the unused space after the existing android namespace IDs. This will work fine until new IDs are added in the framework, at which point your internal ID will overlap with an actual framework ID and cause weird issues.

As for the other instances, those should be generating errors as well. It's possible that there is a bug in AAPT. Regardless, you should remove all instances of @+android from your resources.




回答2:


Though my comment is very late. I faced same issue today, when updated my eclipse and tools. Changed @+android:id to @+id and it solved my issue.



来源:https://stackoverflow.com/questions/26591176/is-there-any-legitimate-use-for-androidid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!