在android的不同xml文件中使用相同的id名称 [英] Using same id names in different xml files in android

查看:64
本文介绍了在android的不同xml文件中使用相同的id名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以在不同的 xml 文件的其他布局中使用相同的 id 名称吗???因为我已经这样做了,也许是因为我得到了一个空指针异常,而我对该 xml 的活动没有开始......

Can i use the same id names in in some other layout in a different xml file??? coz i have done dat that and maybe because of that m getting a nullpointer exception and my activity for that xml does not start...

推荐答案

绝对可以.您可以在任意数量的布局中为相同的 Widget 类型使用相同的 ID.只是同一个布局 XML 中的 ID 不一样.当然不讨论是否推荐这样做.

Absolutely. You can use the same ID's for the same Widget type in any number of layouts that you desire. Just not the same ID in the same Layout XML. Without getting into the debate whether this is recommended or not of course.

让几位开发人员感到困惑的东西,我承认,当我开始时,它也让我感到困惑.例如,当在 Activity 中投射一个 Widget 时,我经常感到困惑,为什么我在几个不同的布局中有相同的 ID 却只有一个匹配的 ID.

What confuses several developers and I confess, when I started, it did confuse me too. When casting a Widget in the Activity, for example, I was often stumped me why I had just one matching ID when I had the same in a few different layouts.

你唯一需要注意的是,例如,考虑这个场景:

The only thing you need to take care of is, for example, consider this scenarior:

我有两个 XML 的名字,比如 layout_1.xmllayout_2.xml 和 2 个对应的 Activities,比如,Act1Act2.现在,两者都具有相同的 Widget,例如在两个 XML 中具有相同 ID 的相同 TextView.(虽然我使用了这个例子,但这不是假设.这实际上是我的应用程序中的情况).这与 NullPointer 在他的评论中所说的非常矛盾.我认为他的意思是你不能在同一个 XML 中使用同一个 ID.

I have two XML's named, say layout_1.xml and layout_2.xml and 2 corresponding Activities, named say, Act1 and Act2. Now, both have the same Widget, say the same TextView with the same ID in both XML's. (Although I am using this example, this is not hypothetical. This is actually how it is in an application of mine). This is quite contradictory to what NullPointer says in his comment. I think he meant you can't use the same ID in the same XML.

<TextView
    android:id="@+id/txtFromName"
    style="@style/UserName"
    android:layout_width="fill_parent"
    android:layout_height="0dip"
    android:layout_weight="1"
    android:ellipsize="marquee"
    android:gravity="top|left|center"
</TextView>

现在,当我在我的代码中投射这个时,看看当我开始输入 findViewById(R.id.txtFrom...)

Now, when I am casting this in my code, see what shows up in the suggestions when I start typing the findViewById(R.id.txtFrom...)

当您在多个 XML 文件中使用相同的 ID 时,为什么 NPE 可能是由于对 ID 是什么有些混淆.保持相应的 XML 打开并在投射时检查 ID,你应该很高兴.如果它仍然存在,我建议使用您的 XML 代码、Java 代码和 LogCat 崩溃报告更新您的帖子.

Why the NPE is perhaps when you use the same ID's in multiple XML files is probably due to some confusion on what the ID is. Keep the corresponding XML open and check the ID when casting and you should be good to go. If it still persists, I would suggest updating your post with your XML code, the Java code and the LogCat crash report.

我通过逐字复制 ID 并在投射时使用与 Widget 实例相同的名称来跟踪它.但是,我想这只是我的方式.

I keep track of that by literally copying the ID and using the same name as an instance of the Widget when casting it. But, that's just my way I suppose.

这篇关于在android的不同xml文件中使用相同的id名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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