Android Fragment Get Id,findviewbyid empty

Source: Internet
Author: User

The project needs to keep the title bar, by clicking the button on the left side of the title bar, pop-up drop-down menu, switch to the right side of the interface, so think of using fragment this class, you can switch activity very well, but there are a lot of problems when using:

1

. Public View Oncreateview (layoutinflater inflater, ViewGroup container,                             Bundle savedinstancestate) {        View VI = Inflater.inflate (R.layout.activity_price, container,false);}
    <span style= "color: #ff0000; Background-color:rgb (255, 255, 102);" > The above code is wrong, when creating fragment, such initialization code I forgot to write return value, causing the program error, so go to don't forget to add a return vi;</span>
   2. When using Findviewbyid, get an ID, get in fragment and activity a bit different, slightly unnoticed, it is possible to obtain a null value NULL:
      The following are the wrong uses:
         A.<pre style= "Background-color:rgb (255, 255, 255); Font-family:consolas; font-size:13.5pt; " ><span style= "Color:rgb (102, 14, 122); "><strong></strong></span><pre style=" Background-color:rgb (255, 255, 255); Font-family:consolas; font-size:13.5pt; " >allonehorizontalscrollview
Tableheaderview = (Allonehorizontalscrollview) GetView. Findviewbyid (R.id. horizontalscrollview_1);

     B.
Allonehorizontalscrollview
getactivity. Findviewbyid (r.id. Horizontalscrollview_1);
Such code gets the value tested to be empty, the correct way is to use the above generated VI:
<span style= "Font-family:consolas;" ><span style= "FONT-SIZE:18PX;" ><strong><span style= "color: #660e7a;" >     </span></strong></span></span><pre name= "code" class= "Java" ><span style = "COLOR: #660e7a;" >       View VI = inflater.inflate (R.layout.activity_price, container,false);        Load Font        loadfonts ();        Tableheaderview = (allonehorizontalscrollview) </span><span style= "color: #ff0000; Background-color:rgb (255 , 255, 102); " >vi.</span><span style= "color: #660e7a;" >findviewbyid (r.id.horizontalscrollview_1);        Tabledataview = (allonehorizontalscrollview) </span><span style= "color: #ff0000; Background-color:rgb (255, 255, 102); " >vi.</span><span style= "color: #660e7a;" >findviewbyid (r.id.horizontalscrollview_2);        Tableheaderview.setscrollview (Tabledataview);</span>


    Hope to have a little help to the students who use fragment.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android Fragment Get Id,findviewbyid empty

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.