Note: Do not inflate layout in the OnCreate method of Mainactivity (the Click event in mainactivity is not responding)

Source: Internet
Author: User

Activity_main has passed Setcontentview (R.layout.activity_main); set to Mainactivity,

Do not inflate out the new layout, then Findviewbyid, then draw the control, and then make other settings for the control, including setting the Click event and so on.

Error procedure:

1 @Override2     protected voidonCreate (Bundle savedinstancestate) {3         Super. OnCreate (savedinstancestate);4 Setcontentview (r.layout.activity_main);5 6View view =uiutils.inflate (r.layout.activity_main);7Button btn_download =(Button) View.findviewbyid (r.id.btn_download);8Btn_download.setonclicklistener (NewOnclicklistener () {9             Ten @Override One              Public voidOnClick (View v) { A download (); -             } -         }); the}

Correct practice:

1 @Override2     protected voidonCreate (Bundle savedinstancestate) {3         Super. OnCreate (savedinstancestate);4 Setcontentview (r.layout.activity_main);5 6Button btn_download =(Button) Findviewbyid (r.id.btn_download);7Btn_download.setonclicklistener (NewOnclicklistener () {8             9 @OverrideTen              Public voidOnClick (View v) { One download (); A             } -         }); -}

PS: In fact, this is not very important, because there is very little in the development of mainactivity in the operation of this demand, mainly for beginners practice should pay attention to the place.

Note: Do not inflate layout in the OnCreate method of Mainactivity (the Click event in mainactivity is not responding)

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.