我怎样才能在不同的布局使用重复的ID? [英] How can I use duplicate IDs in different layouts?

查看:302
本文介绍了我怎样才能在不同的布局使用重复的ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个不同的布局为两个不同的活动。有一个在这些布局具有相同ID的按钮:@ + ID / btnOK。当我对其中一个按钮编程设置属性,我收到了 NullPointerException异常。但是,当我改变ID之一,一切都还好。

I have two different layouts for two different Activities. There is a button in each of these layouts with the same id: "@+id/btnOK". When I set a property for one of these buttons programmatically, I get a NullPointerException. But when I change one of ids, everything is okay.

难道真的,我们不能有不同的布局重复的ID在Android的?

Is it really true that we cannot have duplicate IDs in different layouts in android?

推荐答案

在重复的ID在布局主题,从的 Android开发

On the "Duplicate Ids in layouts" topic, extracted from android developers

定义的ID为视图对象创建RelativeLayout的时候是很重要的。在一个相对布局,同级次可以定义它们的布局相对于另一同级视图,这是由独特的ID引用

的ID不需要在整个树的唯一的,但它应该是你正在寻找树的部分内唯一(这往往会对整个树,所以最好在可能的情况是完全唯一的)。

An ID need not be unique throughout the entire tree, but it should be unique within the part of the tree you are searching (which may often be the entire tree, so it's best to be completely unique when possible).

这意味着不同的布局可以声明相同的ID,寿它不是一个最好的做法。

Which means different layouts may declare identical IDs, tho it's not a best practice.

这篇关于我怎样才能在不同的布局使用重复的ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆