[英]Are view ids local to the activity context?
What is the scope of the a view's identifier? 视图标识符的范围是什么?
Is it local to its corresponding activity? 它是否局限于其相应的活动?
For example, if in activity_main.xml I specify 例如,如果在activity_main.xml中,我指定
<EditText android:id="@+id/edit_message" />
Within my DisplayMessageActivity.java would I legally be able to access R.id.edit_message
? 在我的DisplayMessageActivity.java中 ,我可以合法地访问R.id.edit_message
吗?
When I test this I get an exception, but none of the messages are clearly telling me it's due to accessing an "id out of context", or something along those lines. 当我对此进行测试时,我得到了一个例外,但是没有一条消息清楚地告诉我这是由于访问“上下文无关的ID”或类似的东西。 I'm getting errors like java.lang.NullPointerException
我收到类似java.lang.NullPointerException
错误
This question arose as I was following the beginner tutorial: 当我遵循初学者教程时,出现了这个问题:
EDIT Thanks both for clarifying the difference between the scope of R.id
and that of the widget with id R.id.edit_message
(in terms of accessing it by fiewViewById()
). EDIT都感谢您澄清R.id
的范围和ID为R.id.edit_message
的小部件的范围之间的R.id.edit_message
(就通过fiewViewById()
进行访问而言)。
I actually intended to ask "would I be able to call findViewById(R.id.edit_message) in Activity A to access a widget in Activity B". 我实际上是想问“我是否能够在活动A中调用findViewById(R.id.edit_message)来访问活动B中的小部件”。 And from your answers it appears the answer to that question is "no". 从您的答案看来,该问题的答案是“否”。
In my code, I did not include a view with id R.id.edit_message
in the ViewGroup of Activity B. That's probably why I got the errors. 在我的代码中,我没有在活动B的ViewGroup中包含ID为R.id.edit_message
的视图。这可能就是我得到错误的原因。
What is the scope of the a view's identifier? 视图标识符的范围是什么?
The identifier is an integer. 标识符是整数。 It does not have a scope. 它没有范围。
Within my DisplayMessageActivity.java would I legally be able to access R.id.edit_message? 在我的DisplayMessageActivity.java中,我可以合法地访问R.id.edit_message吗?
If we assume that you mean "would I be able to call findViewById(R.id.edit_message)
to access a widget?", you can certainly call it. 如果我们假设您的意思是“我是否可以调用findViewById(R.id.edit_message)
来访问小部件?”,则可以肯定地调用它。 Whether you get null
or not will depend on whether the activity has a widget in its view hierarchy with that ID. 是否为null
取决于活动在其视图层次结构中是否具有带有该ID的小部件。
However, Activity A cannot call findViewById()
to retrieve a widget from Activity B. The id is an integer and has no scope. 但是,活动A无法调用findViewById()
从活动B中检索小部件。该ID是整数,没有作用域。 However, findViewById()
has a scope: the view hierarchy of whatever you call it on (eg, an activity, a ViewGroup
). 但是, findViewById()
的作用域是:您对其调用的对象(例如,活动, ViewGroup
)的视图层次结构。
Any id that you enter using the XML is processed and placed into a unique integer value in the R.java
file (which you should not edit). 您使用XML输入的任何id都会被处理,并放入R.java
文件中的唯一整数值中(您不应编辑)。 ID values, in particular, go into R.id
, just like the layout XML files you have would be identified by R.layout
. ID值尤其进入R.id
,就像R.layout
标识您拥有的布局XML文件一样。
To answer your question, the scope is global. 为了回答您的问题,范围是全球性的。 At least wherever you can 至少在任何地方
import your.package.name.R;
For example, you have two Activities that each have an EditText
with android:id="@+id/edit_message"
. 例如,您有两个活动,每个活动都有一个带有android:id="@+id/edit_message"
的EditText
。
In that scenario, you can use findViewById(R.id.edit_message)
in both Activities, but the value of R.id.edit_message
is the same. 在那种情况下,您可以在两个活动中使用findViewById(R.id.edit_message)
,但是R.id.edit_message
的值是相同的。
As far as "scoping" goes, findViewById()
is the shorthand of YourActivity.this.findViewById()
, so you would be searching the content view of the current Activity. 就“作用域”而言, findViewById()
是YourActivity.this.findViewById()
的简写,因此您将搜索当前Activity的内容视图。
For the reason you get the exception. 由于这个原因,您得到了例外。 Hard to say. 很难说。 One possibility is that you used the wrong XML layout for setContentView()
and findViewById()
returned you null. 一种可能是您对setContentView()
使用了错误的XML布局,并且findViewById()
返回null。 You can read on the documentation for why that is. 您可以阅读文档以了解原因。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.