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