Updated to Android 4.4, wrote a small program. The discovery is not working. Throws a null pointer exception. In debug mode, it is found that getting the button in the OnCreate method is null.
Android 4.4 has reorganized the layout and put it all under fragment, and fragment has not been called when the OnCreate method executes, so getting the button in the fragment in this method is a null value. There is a fragment inner class in the new class, and when the XML fragment is loaded, the Oncreateview method of the inner class is called, so all operations that were previously in layout and now moved to fragment are moved to this method.
Package Com.bignerdranch.android.geoquiz;import Android.os.bundle;import Android.support.v4.app.fragment;import Android.support.v7.app.actionbaractivity;import Android.view.layoutinflater;import Android.view.Menu;import Android.view.menuitem;import Android.view.view;import Android.view.viewgroup;import Android.widget.Button;import Android.widget.toast;public class Testactivity extends Actionbaractivity {private static Button mfirstbutton;private Static Button Msecondbutton; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Setcontentview (r.layout.activity_test); if (savedinstancestate = = null) { Getsupportfragmentmanager (). BeginTransaction (). Add (R.id.container, New Placeholderfragment ()). commit ();}} @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; This adds items to the action bar if it is PR Esent.getmenuinflater (). Inflate (r.menu.test, menu); return true;} @Overridepublic boolean onoptionsitemselected (MenuItem item) {//Handle Action Bar item clicks here. The action bar will//automatically handle clicks on the Home/up button so long//as you specify a parent activity in and RoidManifest.xml.int id = item.getitemid (); if (id = = r.id.action_settings) {return true;} return super.onoptionsitemselected (item);} /** * A placeholder fragment containing a simple view. */public Static class Placeholderfragment extends Fragment {public placeholderfragment () {} @Overridepublic View Oncreateview (Layoutinflater inflater, ViewGroup container,bundle savedinstancestate) {final View RootView = Inflater.inflate (R.layout.fragment_test, container,false); Mfirstbutton = (Button) Rootview.findviewbyid (R.id.first _button); Mfirstbutton.setonclicklistener (new View.onclicklistener () {@Overridepublic void OnClick (View v) { Toast.maketext (Rootview.getcontext (), R.string.first_toast,toast.length_short). Show ();}}); Msecondbutton = (Button) Rootview.findviewbyid (R.id.second_button); Msecondbutton.setonclicklistener (new View.onclicklisteneR () {@Overridepublic void OnClick (View v) {toast.maketext (Rootview.getcontext (), r.string.second_toast,toast.length_ Short). Show ();}}); return Rootview;}}}