Return of android:actionbaractivity Findviewbyid is null

Source: Internet
Author: User

Due to the continuous upgrade of Android version, the new version will often modify many APIs and other content.

In Api=19, because Mainactivity inherits the Actionbaractivity class and discards the activity previously used,

1  Public class extends Activity    //old version inheritance relationship
1  Public classMainactivityextendsactionbaractivity {//new version of inheritance relationship2 3 @Override4     protected voidonCreate (Bundle savedinstancestate) {5         Super. OnCreate (savedinstancestate);6 Setcontentview (r.layout.activity_main);7 8         if(Savedinstancestate = =NULL) {9 Getsupportfragmentmanager (). BeginTransaction ()Ten. Add (R.id.container,Newplaceholderfragment ()). commit (); One         } A     }
................ -}

At the same time, the layout file in res of the resource file is also redesigned, changing a main.xml to two XML files now: Activity.xml and Fragment_main.xml

Therefore, if you apply the old version of the operation directly in the new version of the implementation, it will be possible to generate a bug; For example: calling a function Findviewbyid

In the older version, this function can be called directly in the onCreate function, but not in the new version, and the return value directly called by this function will be null;

To call this function, you need to overload the OnStart () function, which is called in OnStart () Findviewbyid

1 @Override2     protected voidonCreate (Bundle savedinstancestate) {3         Super. OnCreate (savedinstancestate);4 Setcontentview (r.layout.  Activity_main);5 6         if(Savedinstancestate = =NULL) {7 Getsupportfragmentmanager (). BeginTransaction ()8. Add (R.id.container,Newplaceholderfragment ()). commit ();9         }Ten     } One      A     protected voidOnStart () { -         Super. OnStart (); -          theButton Mbutton = (button) This. Findviewbyid (r.id.button1); -Mbutton.setonclicklistener (NewOnclicklistener () { -              Public voidOnClick (View v) -             { +Intent Intent =NewIntent (); -Intent.setclass (mainactivity. This, Anotheractivity.class); + startactivity (intent); A             } at         }); -}

Reason:

Personal analysis, most likely due to the original layout file layouts in the Main.xml split into Activity.xml and fragment_main.xml, and caused;

Because Setcontentview is called in OnCreate (r.layout. Activity_main), this function simply sets the activity_main. Xml to the app view, but at this point the program has not yet imported the fragment_main.xml;

so that only the onstart cycle is allowed to get activity_main. Component resources in XML

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.