android - Getting error in viewpager adapter,java.lang.IllegalStateException: The specified child already has a parent -
i trying use viewpager images shows 1 picture if slides crashes, saying java.lang.illegalstateexception: specified child has parent. must call removeview() on child's parent first. adapter code is:
public class newdemoadapter extends pageradapter { public arraylist<integer> imagelist; public arraylist<string> lines; public context context; private imageview imageview; private bitmapfactory.options bounds; private viewgroup viewpager; private bitmap cropimg; private layoutinflater minflater; public newdemoadapter(context context, arraylist<integer> imagelist, arraylist<string> line) { this.imagelist = imagelist; this.lines = line; } @override public int getcount() { return imagelist.size(); } public int getitemposition(object object) { return position_none; } @override public boolean isviewfromobject(view view, object object) { return view == ((linearlayout) object); } @override public object instantiateitem(viewgroup container, final int position) { minflater = layoutinflater.from(appcontroller.getappcontext()); final view itemview = minflater.inflate(r.layout.demolayout,container,false); imageview imageview = (imageview) itemview.findviewbyid(r.id.myimage); textview textview = (textview) itemview.findviewbyid(r.id.line); textview.settext(lines.get(position)); imageview.setimageresource(imagelist.get(position)); ((viewpager) container).addview(imageview, 0); return itemview; } @override public void destroyitem(viewgroup container, int position, object object) { ((viewpager) container).removeview((imageview) object); ((viewpager) container).removeview((textview) object); } }
the issue line
((viewpager) container).addview(imageview, 0);
imageview
(r.id.image) has parent itemview
, means can't have viewpager
parent. change
((viewpager) container).addview(imageview, 0);
to
((viewpager) container).addview(itemview, 0);
edit
@override public void destroyitem(viewgroup container, int position, object object) { container.removeview((view) object); }
Comments
Post a Comment