You cannot know the findViewById.

Source: Internet
Author: User

Since learning android's hello world

We can see this function findViewById (), which has become a household name. It is a must-have function (well, this is a nonsense)

But I have never used it in detail, until I found an inexplicable bug caused by this function when writing a program, so I decided to study the next function ~

The findViewById () function we call actually has two types (currently I only see two types, not sure there are other types), one is the findViewById () function in the Activity Class

The other is the findViewById () function defined in the View class.

Generally, the (** view) findViewById (R. id. **) used in the oncreate () method is the findViewById () function in the called Activity.

In other cases, *** view. findViewById () calls findViewById () in the view class ()

Let's take a look at the implementation methods and descriptions in the source code respectively.

FindViewById () defined in the Activity Class ()

[Java]
/**
* Finds a view that was identified by the id attribute from the XML that
* Was processed in {@ link # onCreate }.
*
* @ Return The view if found or null otherwise.
*/
Public View findViewById (int id ){
Return getWindow (). findViewById (id );
}
/**
* Retrieve the current {@ link android. view. Window} for the activity.
* This can be used to directly access parts of the Window API that
* Are not available through Activity/Screen.
*
* @ Return Window The current window, or null if the activity is not
* Visual.
*/
Public Window getWindow (){
Return mWindow;
}
From this, we can see that this function is looking for the object with the specified id defined in xml.
FindViewById () in the View class ()

[Java]
/**
* Look for a child view with the given id. If this view has the given
* Id, return this view.
*
* @ Param id The id to search.
* @ Return The view that has the given id in the hierarchy or null
*/
Public final View findViewById (int id ){
If (id <0 ){
Return null;
}
Return findViewTraversal (id );
/**
* {@ Hide}
* @ Param id the id of the view to be found
* @ Return the view of the specified id, null if cannot be found
*/
Protected View findViewTraversal (int id ){
If (id = mID ){
Return this;
}
Return null;
}
From this we can see that we are looking for objects with the specified id from a child view of a view, so even if the View IDs in several layout XML files are the same, as long as they do not have the same parent node or have the same father's day node but are not on the parent node or above, it is no problem to call findViewById to find them by id. (This sentence is referenced from here http://www.bkjia.com/kf/201204/127404.html)
FAQs about using this function:
1. Since findViewById () in the Activity is to find something from R. java, we need to eliminate controls with the same name.
This is the cause of today's bug.





Speaking of the control naming, there is a small discovery today

Take a closer look at the two sections of code below

[Xml]
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: id = "@ + id/LinearLayout"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"
Android: orientation = "vertical">
</LinearLayout>
[Xml]
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"
Android: orientation = "vertical">
</LinearLayout>
In a section, Layout does not have the id parameter. In a section, there is an id. Although the code is different, it is displayed in outline


 

In this way, the LinearLayout control can be found in the R. id in the first case, and the second one is none. These are also the details to pay attention to in the future.

2. The findViewById () in the call view must think about who the parent View is! That is, ** view in ** view. findViewById () is correct. If no parent View is found, null is returned.

Well, let's get there today. It's a lunch break to write this blog ~

 

From Libaier

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.