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)