Android viewgroup should pay attention to a problem when deleting a subview

Source: Internet
Author: User

Create a linearlayout in the activity, create a scrollhorizon object (scrollhorizon inherited from viewgroup), and call the createlayout function in scrollhorizon to load the myview (inherited from view) object, call the deleteallview function to delete all views.

ScrollhorizonCode:

Public class scrollhorizon extends viewgroup {private context; Public scrollhorizon (context) {super (context); this. context = context; createlayout (); deleteallview () ;}@ overrideprotected void onlayout (Boolean changed, int L, int T, int R, int B) {int childcount = This. getchildcount (); int childleft = 0; int childtop = 0; For (INT I = 0; I <childcount; I ++) {view child = getchildat (I); child. setvisibility (view. visible); child. measure (R-L, B-T); child. layout (childleft, childtop, childleft + child. getmeasuredwidth (), childtop + child. getmeasuredheight (); If (childleft <= 320) {childleft = childleft + child. getmeasuredwidth ();} else {childleft = 0; childtop = childtop + child. getmeasuredheight () ;}} public void createlayout () {resources resource = This. getresources (); packagemanager pmanager = context. getpackagemanager (); Final intent mainintent = new intent (intent. action_main, null); mainintent. addcategory (intent. category_launcher); List <resolveinfo> List = pmanager. queryintentactivities (mainintent, 0); For (INT I = 0; I <list. size (); I ++) {int icon = R. drawable. contacts_button_normal; linearlayout Linear = new linearlayout (context); linear. setlayoutparams (New layoutparams (45, 45); linear. setbackgrounddrawable (this. getresources (). getdrawable (R. drawable. icon); imageview image2 = new imageview (context); image2.setbackgrounddrawable (this. getresources (). getdrawable (R. drawable. icon); drawable = resource. getdrawable (icon); imageview image = new imageview (context); image. setbackgrounddrawable (drawable); linear. addview (image2); linear. addview (image); this. addview (linear) ;}} public void deleteallview () {int size = This. getchildcount (); For (INT I = 0; I <size; I ++) {This. removeviewat (I );}}}
 

Myview code:

 
Public class myview extends view {public myview (context) {super (context); setlayoutparams (New layoutparams (40, 40 ));}}

Main activity code:

 
Public class scrollhorizontest extends activity {public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); linearlayout Linear = new linearlayout (this); scrollhorizon horizon = new scrollhorizon (this); linear. addview (Horizon); this. setcontentview (linear );}}

Display result:

If you do not call the deleteallview functionProgramNo problem. It can be displayed normally, but a null pointer exception occurs when the deleteallview function is called.

cause: After you delete the first view (assuming there are two views in total), there is only one subview in the current viewgroup, and the position of this subview changes to 0, therefore, when removeviewat (1) is called, a null pointer exception occurs. The correct deletion method should be to change removeviewat (I) in the deleteallview function to removeviewat (0);

Related Article

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.