[英]Telling the Difference between two Android GUI objects with the same ID
Say we have two XML files, of which these are snippets: 假设我们有两个XML文件,它们是片段:
firstxml.xml
<?xml version="1.0" encoding="utf-8"?>
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/button1 />
secondxml.xml
<?xml version="1.0" encoding="utf-8"?>
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/button1 />
In our onCreate()
method: 在我们的
onCreate()
方法中:
//Assume all packages necessary are imported
public class Example
{
public void onCreate(Bundle sis)
{
super.onCreate(sis);
setContentView(R.layout.firstxml);
Button thisButton = (Button) findViewbyId(R.id.button1);
}
}
Which button is called and instantiated when this code runs? 该代码运行时将调用并实例化哪个按钮? Since the two buttons are in two different files, will the button in
firstxml.xml
be called because it is the content view of the function? 由于这两个按钮位于两个不同的文件中,因此
firstxml.xml
的按钮firstxml.xml
会被调用,因为它是函数的内容视图?
Thanks in advance for your answers! 预先感谢您的回答!
When you set the firstxml to be the content view, that layout gets inflated. 当将firstxml设置为内容视图时,该布局会膨胀。 findViewById will find the id's in the current layout that's inflated rather than on every layout.
findViewById会在当前已布局的布局(而不是每个布局)中找到ID。
setContentView method description kind of answers your question. setContentView方法描述可以回答您的问题。
The code will call and instantiate the button1 set on your firstxml.xml file. 该代码将调用并实例化firstxml.xml文件中设置的button1。
Note For future readers. 注意供将来的读者使用。 If the layout file doesnot contain button1 it will throw null pointer exception and the editor wont know that there is an error.
如果布局文件不包含button1,它将抛出空指针异常,并且编辑器将不知道存在错误。 This is because R.id.button1 is static field in your R.java class.
这是因为R.id.button1是R.java类中的静态字段。 Also provide better naming convention to your variables for maintainability.
还为变量提供更好的命名约定,以实现可维护性。
Which button is called and instantiated when this code runs?
Since you used the firstxml.xml
as the layout of the activity you are calling the reference of that button from that xml not the button from the secondxml.xml
. 由于您将
firstxml.xml
用作活动的布局,因此您要从xml调用该按钮的引用,而不是secondxml.xml
的按钮。
And in your R.java it will only generate one instance of that id. 并且在您的R.java中,它将仅生成该ID的一个实例。
sample: 样品:
public static final class id {
public static final int button1=0x7f090009;
}
so when you used that reference of that button it will first find/check the layout's id if it exist if it does not exist then it will throw NPE . 因此,当您使用该按钮的引用时,它将首先查找/检查布局的ID(如果存在)(如果不存在),则它将引发NPE 。
You are referencing the the first XML layout file R.layout.firstxml
in the activity's onCreate(Bundle...)
method, so the search here will be made. 您正在活动的
onCreate(Bundle...)
方法中引用第一个XML布局文件R.layout.firstxml
,因此将在此处进行搜索。
Any call to findViewById(int id);
任何对
findViewById(int id);
调用findViewById(int id);
will search in your inflated layout R.layout.firstxml
. 会在您的膨胀布局
R.layout.firstxml
搜索。
The R.java
file is automatically generated when you are defining/adding a new view to your layout. 当您在布局中定义/添加新视图时,将自动生成
R.java
文件。
You can use the same id multiple times, but not in the same layout ! 您可以多次使用相同的ID,但不能在同一布局中使用 !
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.