Changes to the Android4.4 problem

Source: Internet
Author: User

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;}}}

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.